Created
January 29, 2014 09:21
-
-
Save marqsm/8684398 to your computer and use it in GitHub Desktop.
Marionette CompositeView boilerplate - from http://jsfiddle.net/craigjennings11/ddL4n/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Model = Backbone.Model.extend({}); | |
var CollectionModel = Backbone.Model.extend({}); | |
var Collection = Backbone.Collection.extend({ | |
model: CollectionModel | |
}); | |
var View = Backbone.Marionette.ItemView.extend({ | |
tagName : 'li', | |
template: _.template("Number = <%= number %>") | |
}); | |
var CompositeView = Backbone.Marionette.CompositeView.extend({ | |
template: _.template("<h1>Count = <%=count%></h1> <ul class='items'></ul>"), | |
itemView: View, | |
itemViewContainer: '.items', | |
collectionEvents: { | |
'add': 'refresh' | |
}, | |
refresh: function() { | |
this.model.set('count', collection.length); | |
this.render(); | |
} | |
}); | |
var region = new Backbone.Marionette.Region({ el: '#region'}); | |
var collection = new Collection([{number: '1'}, {number: '2'}]); | |
var model = new Model({count : collection.length}); | |
var view = new CompositeView({model: model, collection: collection}); | |
region.show(view); | |
var t = setTimeout(function() { | |
collection.add({number: '3'}); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment