Skip to content

Instantly share code, notes, and snippets.

@rhannequin
Last active December 14, 2015 13:08
Show Gist options
  • Save rhannequin/5091282 to your computer and use it in GitHub Desktop.
Save rhannequin/5091282 to your computer and use it in GitHub Desktop.
Understanding of Backbone architecture with Models, Views and Collections.
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