Skip to content

Instantly share code, notes, and snippets.

@antongudkov
Created June 22, 2014 19:56
Show Gist options
  • Save antongudkov/d9c57c4f5539a5b61b80 to your computer and use it in GitHub Desktop.
Save antongudkov/d9c57c4f5539a5b61b80 to your computer and use it in GitHub Desktop.
var SomeModelView = Backbone.View.extend({
initialize: function() {
this.model.on('change', this.render, this);
},
render: function() {
// render a template
}
});
initialize: function() {
this.listenTo(this.model, 'change', this.render);
}
var SomeCollectionView = Backbone.View.extend({
initialize: function() {
var self = this;
this._views = [];
// create a sub view for every model in the collection
this.collection.each(function(model) {
self._views.push(new SomeModelView({
model: model
}));
});
},
render: function() {
var self = this;
this.$el.empty();
// render each subview, appending to our root element
_.each(this._views, function(subview) {
self.$el.append(subview.render().el);
});
}
});
render: function() {
this.$el.empty();
var container = document.createDocumentFragment();
// render each subview, appending to our root element
_.each(this._views, function(subview) {
container.appendChild(subview.render().el)
});
this.$el.append(container);
}
someMethod: function(){
$('foo').bar();
}
someMethod: function(){
this.trigger('someEvent');
}
this.listenTo view1, 'someEvent', this.someMethodCallback
someMethodCallback: function(){
this.$('foo').bar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment