我是新手,MongoDB的骨干,所以我试着去了解他们,但做起来很难。 我有一个很大很大的问题:我不明白如何操作在Backbone.Model属性在视图中只使用我需要什么。 更具体的-我有一个模型 :
window.User = Backbone.Model.extend({
urlRoot:"/user",
idAttribute: "_id",
defaults: {
_id: null,
name: "",
email: "foo@bar.baz"
}
});
window.UserCollection = Backbone.Collection.extend({
model: User,
url: "user/:id"
});
我有一个观点 :
beforeSave: function(){
var self = this;
var check = this.model.validateAll();
if (check.isValid === false) {
utils.displayValidationErrors(check.messages);
return false;
}
this.saveUser();
return false;
},
saveUser: function(){
var self = this;
console.log('before save');
this.model.save(null, {
success: function(model){
self.render();
app.navigate('user/' + model.id, false);
utils.showAlert('Success!', 'User saved successfully', 'alert-success');
},
error: function(){
utils.showAlert('Error', 'An error occurred while trying to save this item', 'alert-error');
}
});
}
我必须从不同的“_id”任何领域使用“放”方法白衣的数据,所以它必须是水木清华这样的:
{"name": "Foo", "email": "foo@bar.baz"}
但每一次,不取决于我做什么它发送
{**"_id": "5083e4a7f4c0c4e270000001"**, "name": "Foo", "email": "foo@bar.baz"}
从服务器这个错误:
MongoError:不能改变旧文档的_id:{_id:物件( '5083e4a7f4c0c4e270000001'),名称: “富”}新:{_id: “5083e4a7f4c0c4e270000001”,名称: “酒吧”,邮件: “foo@bar.baz” }
Github上链接: https://github.com/pruntoff/habo
提前致谢!