Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created January 17, 2012 20:30
Show Gist options
  • Save jmarnold/1628702 to your computer and use it in GitHub Desktop.
Save jmarnold/1628702 to your computer and use it in GitHub Desktop.
Found the newer one
Backbone.Model.prototype.dependentProperty = function (name, origin, evaluator) {
var update = function () {
var value = evaluator.call(this);
var hash = {};
hash[name] = value;
this.set(hash);
};
this.bind('change:' + origin, function () {
update.call(this);
});
update.call(this);
};
describe('BackboneDependentPropertyTester', function () {
it('should_update_when_target_updates', function () {
var model = new (Backbone.Model.extend({
initialize: function () {
this.dependentProperty('y', 'x', function () {
return this.get('x') * 100;
});
}
}));
model.set({ x: 5 });
expect(model.get('y')).toEqual(500);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment