Last active
December 14, 2015 13:08
-
-
Save rhannequin/5091282 to your computer and use it in GitHub Desktop.
Understanding of Backbone architecture with Models, Views and Collections.
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 ModelView = Backbone.View.extend({ | |
initiliaze: function () { | |
_.template($('#model-template').html()); | |
}, | |
render: function () { | |
return this.template(this.model.toJSON()); | |
} | |
}); | |
var List = Backbone.Collection.extend({ | |
model: Model | |
}); | |
var ListView = Backbone.View.extend( | |
el: '#list' | |
initialize: function() { | |
this.collection = new List(); | |
}, | |
render: function () { | |
var content = ''; | |
_.map(this.collection.models, function (model) { | |
var content += new ModelView({ model: new Model() }).render(); | |
}); | |
this.$el.html(content); | |
} | |
); | |
var MainView = Backbone.View.extend( | |
initialize: function() { | |
this.list = new ListView(); | |
} | |
); | |
var mainView = new MainView(); | |
mainView.list.collection.add( new Model() ); | |
mainView.list.render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment