backbone.stickit和HTML格式:如何保存(补丁)仅更改的属性? [英] backbone.stickit and html-form: How to save (patch) only changed attributes?
问题描述
TL;博士
如何使用backbone.stickit与HTML表单从服务器更改现有模型中取出,仅补丁更改的属性(由用户输入更改HTML表单内)到服务器?
How to use backbone.stickit with a html form to change an existing model fetched from the server and only PATCH the changed attributes (changed by user input within the html form) to the server?
/ TL;博士
我使用 backbone.stickit 在Backbone.js的应用模型绑定一个HTML的形式是一个骨干视图的一部分。这工作得很好,到目前为止,但它变得有点有点复杂,如果我要拯救绑定模型。这是因为我想用补丁的方法,只发送更改的属性到服务器。我试图说明什么我迄今所做的:
I'm using backbone.stickit in a backbone.js application to bind a model to a HTML-form which is part of a backbone view. This works fine so far, but it becomes a little bit complicated if I'm going to save the bound model. This is because I want to use the PATCH-method and only send the changed attributes to the server. I try to illustrate what I've done so far:
撷取来自服务器模型
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()
方法后得到这就需要在服务器上补丁的属性。所以,我想这个解决方法,我发现<一个href=\"http://stackoverflow.com/questions/14485366/avoid-fetch-marking-model-attributes-as-changed\">here
The last line indicates my problem, because I thought I can used the changedAtrributes()
method later to get the attributes which need a patch on the server. So I tried this workaround which I found here
user.fetch({
success: function (model, response, options) {
model.set({});
}
});
user.changedAtrributes(); //Returns now "false"
做stickit绑定
现在我使我的看法,并调用 stickit()
方法上来看,做绑定:
Now I render my view and call the stickit()
method on the view, to do the bindings:
//Bindings specified in the view:
[...]
bindings: {
"#username" : "username"
"#age" : "age"
}
[...]
//within the render method of the view
this.stickit();
该绑定正常工作和我的用户模型被更新,但 changedAttributes()
保持为空所有的时间。
模型保存到服务器
如果用户已经进行的所有必要的修改,该模型应该被保存到服务器。我想使用PATCH方法,只发送更改的属性到服务器上。
If the user has made all required changes, the model should be saved to the server. I want to use the PATCH method and only send the changed attributes to the server.
user.save(null, {patch:true}); //PATCH method is used but ALL attributes are sent to the server
或
user.save(user.changedAttributes(),{patch : true});
在第二种方法有不同的结果:
With the second approach there are different outcomes:
- 如果我没有使用
user.set({})
woraround,所有的属性得到修补到服务器 - 如果我用
changedAttributes的
是假,所有属性都投入到服务器user.set({})
woraround返回值() - 如果我称之为
user.set(时代,123)
之前调用保存()
,那么只有年龄属性被修补到服务器
- if I didn't use the
user.set({})
woraround, all attributes get PATCHED to the server - if I use the
user.set({})
woraround the return value ofchangedAttributes()
is "false" and all attributes are PUT to the server - if I call a
user.set("age","123")
before callingsave()
, then only the age attribute is PATCHED to the server
所以,结果3是我期望的行为,但也有2个问题与这样的:首先stickit似乎并没有使用设置()
方法对模型进行更新的属性,如果他们的HTML表单内改变。第二,如果你调用设置()
有一个属性,后来与另一个,只有第二个属性是由返回changedAttributes()
。
So outcome 3 is my desired behaviour, but there are 2 problems with this: First stickit doesn't seem to use the set()
method on the model to update the attributes if they are changed within the html-form. And second, if you call set()
with one attribute and afterwards with another, only the second attributes is returned by changedAttributes()
.
也许我只是在监督的骨干或backbone.stickit文档什么的,所以我没有得到预期的行为工作。有关任何想法?
Maybe I just overseen something in the backbone or backbone.stickit docs, so I didn't get the desired behaviour working. Any ideas about that?
推荐答案
注意:由于发现的问题没有直接关系backbone.stickit,更多的骨干本身
NOTE: As found out the problem wasn't directly related to backbone.stickit, more to backbone itself.
解决我自己的这个问题,也许这可以帮助别人谁可能就这个问题绊倒:
Solved this problem on my own, maybe this helps someone who may stumble upon this question:
主干只跟踪的不变的属性,但不是的未保存的属性。因此,与
Backbone only keep track of unchanged attributes, but not of unsaved attributes. So with
model.changedAttributes();
你只会得到的模式,因为其中最后被改变的属性。
you will only get the attributes of the model, which was changed since the last
model.set("some_attribute","some_value")
最后,我偶然发现了 backbone.trackit 这是由创作者保持着Backbone.js的插件的backbone.stickit。有了这个插件,你可以跟踪的未保存的属性,然后(自上次 model.save已改变()
所有属性)中保存使用它们模型的-method。示例(我的用例):
Finally I stumbled upon backbone.trackit which is a backbone.js plugin maintained by the creator of backbone.stickit. With this plugin you can track unsaved attributes (all attributes which have changed since the last model.save()
) and then use them in the save-method of a model. Example (my usecase):
Backbone.View.extend({
bindings: {
"#name" : "name",
"#age" : "age"
},
initialize: function () {
this.model = new User();
this.model.fetch({
success: function (model, response, options) {
//this tells backbone.stickit to track unsaved attributes
model.startTracking();
}
});
},
render: function () {
this.$el.html(tmpl);
this.stickit();
return this;
},
onSaveUserToServer: function () {
//first argument: only unsaved attributes, second argument: tell backbone to PATCH
this.model.save(this.model.unsavedAttributes(), { patch: true });
});
});
这篇关于backbone.stickit和HTML格式:如何保存(补丁)仅更改的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!