Skip to content

Commit 795d6b9

Browse files
committed
conditional dependency tracking
1 parent 5a3ec47 commit 795d6b9

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/exp-parser.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,14 @@ module.exports = {
104104
return makeGetter('return ' + exp, exp)
105105
}
106106
vars = utils.unique(vars)
107-
var pathRE = new RegExp("\\b(" + vars.join('|') + ")[$\\w\\.]*\\b", 'g'),
108-
body = 'return ' + exp.replace(pathRE, function (path) {
109-
return 'this.' + getRel(path, compiler) + path
107+
var accessors = '',
108+
pathRE = new RegExp("\\b(" + vars.join('|') + ")[$\\w\\.]*\\b", 'g'),
109+
body = 'return ' + exp.replace(pathRE, function (path) {
110+
var val = 'this.' + getRel(path, compiler) + path
111+
accessors += val + ';'
112+
return val
110113
})
114+
body = accessors + body
111115
return makeGetter(body, exp)
112116
}
113117
}

test/functional/fixtures/expression.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
</form>
1919
<button sd-on="click: two.three = 'clicked'">click</button>
2020
</div>
21+
<div id="conditional">
22+
<p>{{ok ? yesMsg : noMsg}}</p>
23+
<button sd-on="click: ok = !ok" class="toggle">toggle</button>
24+
<button sd-on="click: noMsg = 'Nah'" class="change">change</button>
25+
</div>
2126
<script>
27+
Seed.config({debug:true})
2228
var normal = new Seed({
2329
el: '#normal',
2430
scope: {
@@ -39,6 +45,15 @@
3945
}
4046
}
4147
})
48+
49+
var conditional = new Seed({
50+
el: '#conditional',
51+
scope: {
52+
ok: true,
53+
yesMsg: 'YES',
54+
noMsg: 'NO'
55+
}
56+
})
4257
</script>
4358
</body>
4459
</html>

test/functional/specs/expression.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global normal */
22

3-
casper.test.begin('Expression', 16, function (test) {
3+
casper.test.begin('Expression', 19, function (test) {
44

55
casper
66
.start('./fixtures/expression.html', function () {
@@ -47,6 +47,15 @@ casper.test.begin('Expression', 16, function (test) {
4747
test.assertField('four', 'clicked')
4848
test.assertSelectorHasText('#lazy p', 'three clicked!')
4949

50+
// conditional expression
51+
// e.g. ok ? yesMSg : noMsg
52+
// make sure all three are captured as dependency
53+
test.assertSelectorHasText('#conditional p', 'YES')
54+
this.click('#conditional .toggle')
55+
test.assertSelectorHasText('#conditional p', 'NO')
56+
this.click('#conditional .change')
57+
test.assertSelectorHasText('#conditional p', 'Nah')
58+
5059
})
5160
.run(function () {
5261
test.done()

0 commit comments

Comments
 (0)