Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.8] Allow a computed value to have multiple dependencies #1099

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/lib/bind/bind-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
var method = expression.slice(0, index);
var args = expression.slice(index + 1, -1).replace(/ /g, '').split(',');
//console.log('%c on [%s] compute [%s] via [%s]', 'color: green', args[0], name, method);
this.addPropertyEffect(model, args[0], 'compute', {
var effect = {
property: name,
method: method
});
method: method,
args: args
};
for (var i = 0; i < args.length; i++){
this.addPropertyEffect(model, args[i], 'compute', effect);
}
};

Bind._notifyChange = function(property) {
Expand Down Expand Up @@ -69,7 +73,7 @@

compute: function(model, source, effect) {
return 'this.' + effect.property
+ ' = this.' + effect.method + '(this._data.' + source + ');';
+ ' = this.' + effect.method + '(this._data.' + effect.args.join(',this._data.') + ');';
},

reflect: function(model, source) {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/bind-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
computed: {
computedvalue: 'computeValue(value)',
computedvaluemultipledependencies: 'computedValueMultipleDependencies(value, bool)',
computednotifyingvalue: 'computeNotifyingValue(notifyingvalue)'
},
bind: {
Expand All @@ -33,6 +34,9 @@
computeValue: function(val) {
return val + 1;
},
computedValueMultipleDependencies: function(val1, val2) {
return val2 ? val1 : -val1;
},
computedvalueChanged: function() {},
notifyingvalueChanged: function() {},
readonlyvalueChanged: function() {},
Expand Down
12 changes: 12 additions & 0 deletions test/unit/bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
el.value = 44;
assert.equal(el.computedvalue, 45, 'Computed value not correct');
});

test('computed value updates with multiple dependencies', function() {
el.bool = true;
el.value = 44;
assert.equal(el.computedvaluemultipledependencies, 44, 'Computed value with multiple dependencies is not correct');

el.value = 45;
assert.equal(el.computedvaluemultipledependencies, 45, 'Computed value with multiple dependencies was not updated');

el.bool = false;
assert.equal(el.computedvaluemultipledependencies, -45, 'Computed value with multiple dependencies was not updated');
});

test('notification sent', function() {
var notified = false;
Expand Down