admin管理员组文章数量:1392002
I've inherited a backbone js based app. I really like backbone and i'm just starting to get my head around it. From my understanding when model.save is called on a new entity it should post that to the server, the server should return the same json but with an id alloted and backbone should persist that id to the model so that further saves result in a PUT with the ID for update.
However, when I call model.save()
and then try to get the model.id property, it's null.
Is this because I'm not doing it with a call back? So the property hasn't been set yet?
How would I set the success callback? calling model.save({success: function(){...}})
doesn't work?
here is the actual call:
model.save(null, {
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
I've inherited a backbone js based app. I really like backbone and i'm just starting to get my head around it. From my understanding when model.save is called on a new entity it should post that to the server, the server should return the same json but with an id alloted and backbone should persist that id to the model so that further saves result in a PUT with the ID for update.
However, when I call model.save()
and then try to get the model.id property, it's null.
Is this because I'm not doing it with a call back? So the property hasn't been set yet?
How would I set the success callback? calling model.save({success: function(){...}})
doesn't work?
here is the actual call:
model.save(null, {
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
Share
Improve this question
edited Sep 28, 2011 at 18:05
Jacob
78.9k24 gold badges157 silver badges241 bronze badges
asked Sep 28, 2011 at 12:32
user156888user156888
6
- Have you tried to set the error callback too? – mamoo Commented Sep 28, 2011 at 12:41
- have you verified that the server does indeed get called? – timDunham Commented Sep 28, 2011 at 13:09
- Did you create your 'model' using Backbone.Model.extend? – mamoo Commented Sep 28, 2011 at 13:10
- is the idAttribute set? this is the way to change the default 'id' to be something else (like, say 'entityId') – timDunham Commented Sep 28, 2011 at 13:14
- can you post the response that your server provides, including the json result and the http header status? – Derick Bailey Commented Sep 28, 2011 at 13:33
3 Answers
Reset to default 4Something feels odd about this. Setting silent: true
only makes it so none of the events get fired. Everything else should happen normally. In other words, don't assume that setting slient: true
is the right answer here...
I suspect you are actually throwing an exception some place (probably with validation or something like that) and somehow, setting silent: true
is causing everything to flow through.
I would strongly suggest that you remove this option and check your console or run with the debugger... I suspect you have a bug lurking around there some place.
Some suggestions: Take a look at the annotated source for the model.set
function. It gets called before your success
callback will get called. Inside of that function, there are several things that will happen if silent
is false. These include validation, individual property change triggers, and a global change trigger. I would bet money that either the validation is failing or something that is listening to the changes is throwing an exception.
i needed to set the silent: true on the save:
model.save(null, {
silent: true,
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
I had the same issue, turned out to be because my custom parse
was failing
本文标签: javascriptbackbonejs modelsave doesn39t set the idStack Overflow
版权声明:本文标题:javascript - backbone.js model.save doesn't set the id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744769369a2624243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论