Skip to content

Instantly share code, notes, and snippets.

@psaitu
Created November 28, 2011 02:32
Show Gist options
  • Save psaitu/1398845 to your computer and use it in GitHub Desktop.
Save psaitu/1398845 to your computer and use it in GitHub Desktop.
backbone1
(function($){
Backbone.sync = function(model, method, options) {
options.success();
}
var Item = Backbone.Model.extend({
defaults: {
name: "itemname",
description: "item description",
rating: 1.0
},
initialize: function() {
console.log(this.toJSON());
}
});
var Items = Backbone.Collection.extend({
model: Item,
localStorage: new Store("items"),
});
var ItemView = Backbone.View.extend({
tagName: 'li',
initialize: function() {
_.bindAll(this, 'render');
//this.model.bind('change', this.render);
//this.model.bind('remove', this.unrender);
this.render();
},
render: function() {
console.log(this.model);
$(this.el).append("<li>" + this.model.get('name') + "</li>");
}
});
var AddItemView = Backbone.View.extend({
el: $('.add-item'),
events: {
'click #add_item': 'add'
},
initialize: function() {
this.collection = new Items();
//this.collection.bind('add', this.appendItem);
this.render();
},
render: function() {
var template = _.template($("#add_item_template").html(), {});
this.el.html(template);
},
add: function(event) {
var a_new_item_name = this.$("#item_name").val();
var a_new_item_description = this.$("#item_description").val();
var a_new_item_rating = this.$("#item_rating").val();
var a_new_item = new Item({
name: a_new_item_name,
description: a_new_item_description,
rating: a_new_item_rating
});
this.collection.add(a_new_item);
this.appendItem(a_new_item);
},
appendItem: function(item) {
var itemView = new ItemView(
{model: item}
);
}
});
var add_item_view = new AddItemView();
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment