私はMongoDBとBackboneの初心者なので、理解しようとしていますが、難しいです。私には大きな問題があります。必要なものだけをビューで使用するために 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」以外のフィールドからのデータを含む「put」メソッドを使用する必要があるため、次のようにする必要があります。
{"name": "Foo", "email": "foo@bar.baz"}
でも毎回、私が何をするかに依存しません。
{**"_id": "5083e4a7f4c0c4e270000001"**, "name": "Foo", "email": "foo@bar.baz"}
サーバーからのこのエラー:
MongoError: ドキュメントの _id を変更できません}
Github リンク: https://github.com/pruntoff/habo
前もって感謝します!