主干模型未正确调用销毁回调
我对这件事快抓狂了。我实在不明白这里出了什么问题。
我有一个称为项目的模型集合,每个项目都有自己的项目视图项。
我试图执行的任务只是删除属于该集合的所有模型。相应的模型视图也应该一路删除。 我是这样做的:
empty: function() {
for(i=0; i<this.length; i++) {
var model = this.at(i);
console.log("script remove: "+model.get('id'));
model.destroy();
};
我确保使用 console.log 输出销毁正确的模型,该输出为我提供了每个模型 id。
在项目模型中,我有这个销毁函数:
destroy: function() {
console.log(this);
console.log("model remove: "+this.get('id'));
return Backbone.Model.prototype.destroy.call(this);
},
我再次确保我处理正确的模型。 我还检查模型的结构以确保回调正确。
Projectviewitem 在模型的初始化方法中初始化:
initialize: function() {
this.view = new ProjectViewItem({model: this});
},
它绑定到模型:
initialize: function() {
this.model.bind('destroy', this.remove, this);
...
它的销毁方法是:
remove: function() {
console.log("remove: "+this.model.get('id'));
return Backbone.Model.prototype.remove.call(this);
},
现在关于 console.log 输出。
这是我清空集合时得到的内容。
script remove: 344
d (model 344 object)
model remove: 344
//here I should get "remove: 340", but I don't. That means the projectviewitem's remove method isn't called, but why?
script remove: 343
d (model 343 object)
model remove: 343
remove: 343
remove: 343
//model 343 makes two callback calls?
结果,所有模型都被删除,但只有一个视图被删除。我不明白模型 344 如何失去其视图的回调以及模型 343 如何获得两个.. 当我检查对象(>> d)时,回调没问题。 344有自己的,343也有,他们似乎都指向他们相应的观点。
有什么线索吗?
在解决这个问题之前,我将 (this.view.remove();) 添加到 model.destroy 方法中。它有效,但我肯定想知道为什么绑定不能正常工作。
I'm getting nuts with this thing. I really can't figure out what's the issue here.
I have a collection of models called projects, and each of this project has its own projectviewitem.
The task I'm trying to perform is just deleting all models belonging to the collection. The corresponding model views should also be removed along the way.
Here is how I do it:
empty: function() {
for(i=0; i<this.length; i++) {
var model = this.at(i);
console.log("script remove: "+model.get('id'));
model.destroy();
};
I'm making sure I'm destroying the right models with the console.log output which is giving me each model id.
In the project model I have this destroy function:
destroy: function() {
console.log(this);
console.log("model remove: "+this.get('id'));
return Backbone.Model.prototype.destroy.call(this);
},
Again I'm making sure I deal with the proper model.
I also inspect the structure of the model to be sure the callback is right.
The projectviewitem is initialized within the model's initialize method:
initialize: function() {
this.view = new ProjectViewItem({model: this});
},
It is bound to the model:
initialize: function() {
this.model.bind('destroy', this.remove, this);
...
And its destroy method is:
remove: function() {
console.log("remove: "+this.model.get('id'));
return Backbone.Model.prototype.remove.call(this);
},
About the console.log output now.
Here is what I get when emptying the collection.
script remove: 344
d (model 344 object)
model remove: 344
//here I should get "remove: 340", but I don't. That means the projectviewitem's remove method isn't called, but why?
script remove: 343
d (model 343 object)
model remove: 343
remove: 343
remove: 343
//model 343 makes two callback calls?
As a result, all models are deleted but only one view is. I don't understand how model 344 loses the callback to its view and how model 343 gets two..
When I check out the objects (>> d), the callbacks are alright. 344 has its own, same for 343, and they seem to be pointing to their corresponding view.
Any clue?
Until I solve this issue I add (this.view.remove();) to the model.destroy method. It works but I would definitely like to know why the binding is not working properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些事情可能会导致您出现问题,但如果没有更多代码就很难确定:
您不能使用递增的
for
循环来删除项目 (destroy() 将从集合中删除该项目),因为您正在更改长度。解决这个问题的经典方法是向后迭代,而不是向前迭代:
具有 id 的模型上的
.destroy()
方法不会触发destroy
除非服务器成功响应(相关代码) 。因此,您需要确保您的服务器对 DELETE 请求发出 200 响应。正如我的评论中所述,我不确定您在视图上调用
Backbone.Model.prototype.destroy.call(this)
想要做什么。我怀疑这是一个拼写错误,无论是在您的帖子中还是在您的代码中,但我不确定它会实现什么。我希望这些问题中至少有一个是您问题的根源。我有一个工作中的 jsFiddle,这里的代码稍作修改: http://jsfiddle.net/e8Uct/3 / - 不过,它仅使用动态创建的模型,因此它不处理服务器回调。
顺便说一句,我个人不喜欢使用模型初始化视图的模式 - 我宁愿反其道而行之,并使模型完全独立于 UI。但这确实是一个品味问题。
There are a few things that could be causing your problems here, though it's hard to know for sure without more code:
You can't use an incrementing
for
loop to delete items (destroy()
will remove the item from the collection), because you're changing the length as you go. The classic way to fix this is to iterate backwards, rather than forwards:The
.destroy()
method on a model with an id won't trigger thedestroy
event unless there's a successful response from the server (relevant code). So you need to make sure your server is responding with a 200 response to the DELETE request.As noted in my comment, I'm not sure what you're trying to do by calling
Backbone.Model.prototype.destroy.call(this)
on a view. I suspect this is a typo, either in your post or in your code, but I'm not sure what it would accomplish.I hope at least one of these issues is the source of your problem. I have a working jsFiddle with a slightly modified version of your code here: http://jsfiddle.net/e8Uct/3/ - it's only using models created on the fly, though, so it's not dealing with the server callback.
As an aside, I personally don't like the pattern of initializing the view with the model - I'd rather do it the other way around, and keep the model wholly independent of the UI. But that's really a matter of taste.