tl;博士
如何使用带有 html 表单的主干.stickit 来更改从服务器获取的现有模型,并且只将更改的属性(由 html 表单中的用户输入更改)修补到服务器?
/tl;博士
我在一个backbone.js 应用程序中使用backbone.stickit将模型绑定到作为主干视图一部分的HTML 表单。到目前为止,这工作正常,但如果我要保存绑定模型,它会变得有点复杂。这是因为我想使用 PATCH 方法并且只将更改的属性发送到服务器。我试图说明我到目前为止所做的事情:
从服务器获取模型
user = new User(); //instatiate a new user-model
user.fetch(); //fetching the model from the server
console.log(user.changedAttributes()); // Returns ALL attributes, because model was empty
最后一行表示我的问题,因为我认为我可以changedAtrributes()
稍后使用该方法来获取需要在服务器上打补丁的属性。所以我尝试了我在这里找到的解决方法
user.fetch({
success: function (model, response, options) {
model.set({});
}
});
user.changedAtrributes(); //Returns now "false"
做stickit-bindings
现在我渲染我的视图并调用视图stickit()
上的方法来进行绑定:
//Bindings specified in the view:
[...]
bindings: {
"#username" : "username"
"#age" : "age"
}
[...]
//within the render method of the view
this.stickit();
绑定工作正常,我的用户模型得到更新,但是changedAttributes()
一直保持空白。
将模型保存到服务器
如果用户进行了所有需要的更改,则应将模型保存到服务器。我想使用 PATCH 方法,并且只将更改的属性发送到服务器。
user.save(null, {patch:true}); //PATCH method is used but ALL attributes are sent to the server
或者
user.save(user.changedAttributes(),{patch : true});
第二种方法有不同的结果:
- 如果我没有使用
user.set({})
woraround,所有属性都会被修补到服务器 - 如果我使用
user.set({})
woraround 的返回值为changedAttributes()
“false”并且所有属性都被 PUT 到服务器 - 如果我
user.set("age","123")
在调用之前调用asave()
,那么只有年龄属性被修补到服务器
set()
所以结果 3 是我想要的行为,但这有两个问题:首先,如果在 html 表单中更改属性,stickit 似乎没有使用模型上的方法来更新属性。其次,如果您set()
使用一个属性调用,然后使用另一个属性调用,则仅返回第二个属性changedAttributes()
。
也许我只是监督了骨干网或骨干网.stickit 文档中的某些内容,所以我没有得到想要的行为。有什么想法吗?