Skip to content

Instantly share code, notes, and snippets.

@1010real
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1010real/35f5adff1c2473d77006 to your computer and use it in GitHub Desktop.
Save 1010real/35f5adff1c2473d77006 to your computer and use it in GitHub Desktop.
backbone note05
// View of Food
var FoodView = Backbone.View.extend({
tagName:'li',
className:'food',
initialize: function() {
this.render();
this.model.on('change', this.render, this); // 追加
this.model.on('destroy', this.remove, this); // 追加
},
// eventsを追加
events:{
'dblclick span.name': 'renderRename',
'keypress input.name': 'save',
'click button.delete': 'destroy'
},
render: function() {
var tmp = $('<span class="name">')
.text(this.model.get('name'))
.after(':')
.after($('<span class="calory">')
.text(this.model.get('calory')))
.after($('<button class="delete">delete</button>')); //ボタンを追加表示
$(this.el).html(tmp);
return this;
},
// renderRenameを追加
renderRename: function() {
var tmp = $('<input class="name">')
.attr('value', this.model.get('name'))
.after($('<span class="calory">')
.text(this.model.get('calory')));
$(this.el).html(tmp);
return this;
},
// saveを追加
save: function(e) {
if (e.keyCode === 13) {
this.model.save('name', $(this.el).find('input.name').val());
}
},
// destroyを追加
destroy: function(e) {
this.model.destroy();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment