如何删除骨干模型
我创建了以下代码,但无法从后端销毁模型。
我收到“模型未定义错误”。
不知道为什么找不到型号?我缺少什么参数?我在这个列表视图中添加这个是错误的吗?我应该将其添加到模型视图中吗?如果是这种情况,我在列表视图中添加了保存新模型,所以不明白为什么我不能将其添加到她。
window.LibraryView = Backbone.View.extend({
tagName: 'section',
className: 'mynotes',
events: {
"keypress #new-title": "createOnEnter",
//"keypress #new-content": "createOnEnter"
"click .mybutton": "clearCompleted"
},
initialize: function() {
_.bindAll(this, 'render');
this.template = _.template($('#library-template').html());
this.collection.bind('reset', this.render);
this.collection.bind('add', this.render);
this.collection.bind('remove', this.render);
},
render: function() {
var $mynotes,
collection = this.collection;
$(this.el).html(this.template({}));
$mynotes = this.$(".mynotes");
collection.each(function(mynote) {
var view = new LibraryMynoteview({
model: mynote,
collection: collection
});
$mynotes.append(view.render().el);
});
return this;
},
createOnEnter: function(e) {
var input = this.$("#new-title");
var input2 = this.$("#new-content");
//var msg = this.model.isNew() ? 'Successfully created!' : "Saved!";
if (!input || e.keyCode != 13) return;
// this.model.save({title: this.$("#new-title").val(), content: this.$("#new-content").val() }, {
var newNote = new Mynote({title: this.$("#new-title").val(), content: this.$("#new-content").val()});
this.collection.create(newNote);
},
clearCompleted: function() {
this.model.destroy();
this.collection.remove();
}
});
I have created the following code and i'm unable to destroy a model from the backend.
I get a 'model is not defined error'.
I don't know why it cannot find a model? what parameters am i missing? am i wrong in adding this in this list view? should i add it in the models view? and if that is the case i added the save new model in the list view so can't see why i can't add it her.
window.LibraryView = Backbone.View.extend({
tagName: 'section',
className: 'mynotes',
events: {
"keypress #new-title": "createOnEnter",
//"keypress #new-content": "createOnEnter"
"click .mybutton": "clearCompleted"
},
initialize: function() {
_.bindAll(this, 'render');
this.template = _.template($('#library-template').html());
this.collection.bind('reset', this.render);
this.collection.bind('add', this.render);
this.collection.bind('remove', this.render);
},
render: function() {
var $mynotes,
collection = this.collection;
$(this.el).html(this.template({}));
$mynotes = this.$(".mynotes");
collection.each(function(mynote) {
var view = new LibraryMynoteview({
model: mynote,
collection: collection
});
$mynotes.append(view.render().el);
});
return this;
},
createOnEnter: function(e) {
var input = this.$("#new-title");
var input2 = this.$("#new-content");
//var msg = this.model.isNew() ? 'Successfully created!' : "Saved!";
if (!input || e.keyCode != 13) return;
// this.model.save({title: this.$("#new-title").val(), content: this.$("#new-content").val() }, {
var newNote = new Mynote({title: this.$("#new-title").val(), content: this.$("#new-content").val()});
this.collection.create(newNote);
},
clearCompleted: function() {
this.model.destroy();
this.collection.remove();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将clearCompleted方法绑定到
this
:You have to bind your clearCompleted method to
this
: