xbo*_*nez 5 javascript backbone.js backbone-model
我有以下代码来创建一个集合的新模型.基础数据存储区是一个远程API:
var postCreationStatus = this.model.create(newPostModel, {
wait : true // waits for server to respond with 200 before adding newly created model to collection
}, {
success : function(resp){
console.log('success callback');
console.log(resp);
},
error : function(err) {
console.log('error callback');
console.log(err);
}
});
Run Code Online (Sandbox Code Playgroud)
创建了新模型,我可以从数据库中确认这一点,但是不会调用成功函数和错误回调函数.
创建完成后,我想重定向用户.过早地重定向会导致AJAX请求失败,这就是为什么我使用成功回调很重要的原因.
服务器以JSON响应{ id : 11 }和HTTP状态响应200 OK.
查看骨干代码,我意识到我对create()函数的调用是不正确的.成功和错误回调需要在作为第二个参数传入的对象内,而不是作为第三个参数.改进的工作片段是这样的:
var postCreationStatus = this.model.create(newPostModel, {
wait : true, // waits for server to respond with 200 before adding newly created model to collection
success : function(resp){
console.log('success callback');
console.log(resp);
that.redirectHomePage();
},
error : function(err) {
console.log('error callback');
// this error message for dev only
alert('There was an error. See console for details');
console.log(err);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4937 次 |
| 最近记录: |