Skip to content

Instantly share code, notes, and snippets.

@Dakuan
Last active December 26, 2015 03:09
Show Gist options
  • Save Dakuan/7084276 to your computer and use it in GitHub Desktop.
Save Dakuan/7084276 to your computer and use it in GitHub Desktop.
Project.ParentModel = Backbone.Model.extend({
initialize: function(args){
// children is a collection
this.set('children', args.children);
this.listenTo(this.get('children'), 'add', this._onChildrenAdd);
},
_onChildrenAdd: function(child, collection){
this.set('total', collection.reduce(function(memo, ch){
return memo + ch.get('value');
}, 0))
}
});
Project.ParentView = Backbone.View.extend({
initialize: function(){
this.listenTo(this.model, 'change:value', this._onModelValueChange);
},
_onModelValueChange: function(model, value){
this.$('some container').text(value);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment