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

Expose bare minimum to add effects dynamically. #3455

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
25 changes: 25 additions & 0 deletions src/standard/effectBuilder.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@
var prop = Polymer.Bind.addPropertyEffect(this, property, kind, effect);
// memoize path function for faster lookup.
prop.pathFn = this['_' + prop.kind + 'PathEffect'];
return prop;
},

addCustomEffect: function(property, fn) {
// In case this is the first effect of the property, we MUST create the
// underlying trigger machinery by hand.
var effects = Polymer.Bind.ensurePropertyEffects(this, property);
if (effects.length === 0) {
// The current value will be masked by the descriptor, read it ...
var val = this[property];
Polymer.Bind._createAccessors(this, property, effects);
// ... and apply on our data store
this.__data__[property] = val;
}
return this._addPropertyEffect(property, 'function', fn);
},

removeCustomEffect: function(property, fx) {
var effects = this._propertyEffects && this._propertyEffects[property];
if (effects) {
var index = effects.indexOf(fx);
if (index !== -1) {
effects.splice(index, 1);
}
}
},

// prototyping
Expand Down
4 changes: 4 additions & 0 deletions src/standard/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@
}
},

_functionPathEffect: function(path, value, effect) {
Polymer.Bind._functionEffect.call(this, path, value, effect);
},

_pathMatchesEffect: function(path, effect) {
var effectArg = effect.trigger.name;
return (effectArg == path) ||
Expand Down
66 changes: 66 additions & 0 deletions test/unit/bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,72 @@
});
});

suite('custom user effects', function() {

Polymer({
is: 'x-custom-effect'
});

test('Add custom effect', function() {
var el = document.createElement('x-custom-effect');

var called = 0;
el.addCustomEffect('foo', function(path, value, old) {
called += 1;
assert.equal(path, 'foo');
assert.equal(value, 'bar');
assert.equal(old, undefined);
});

el.foo = 'bar';
assert.equal(called, 1);
});

test('Remove custom effect', function() {
var el = document.createElement('x-custom-effect');

var called = 0;
var fx = el.addCustomEffect('foo', function() {
called += 1;
});

el.removeCustomEffect('foo', fx);

el.foo = 'bar';
assert.equal(called, 0);
});

test('Ensure old values are sent', function() {
var el = document.createElement('x-custom-effect');
el.foo = 'bar';

var called = 0;
el.addCustomEffect('foo', function(path, value, old) {
called += 1;
assert.equal(old, 'bar');
});

el.foo = 'quux';
assert.equal(called, 1);
});

test('Ensure path effects can be seen', function() {
var el = document.createElement('x-custom-effect');
el.foo = {bar: 'quux'};

var called = 0;
el.addCustomEffect('foo', function(path, value, old) {
called += 1;
assert.equal(path, 'foo.bar');
assert.equal(value, 'quod');
assert.equal(old, undefined); // always undefined for structured paths!
});

el.set('foo.bar', 'quod');
assert.equal(called, 1);
});
});

</script>

</body>
Expand Down