用于 model.save() 的 Backbone.js/express.js 参数 [英] Backbone.js/express.js parameters for model.save()
问题描述
我在客户端使用 Backbone.js,在后端使用 node.js,但在进行有限"模型保存时遇到了一些麻烦,如下所述:http://backbonejs.org/#Model-save
如示例所示,如果我这样做
book.save({author: "Teddy"});
如何使用 express.js 访问保存的参数,即我只想保存到作者"字段的事实?我已经尝试了以下
req.body ->给出正在保存的模型的所有参数,我只想要作者"字段req.query ->空的
非常感谢任何帮助!
保存 model.save([attributes], [options])
[...] 属性散列(如集合)应该包含你想要改变的属性——未提及的键不会被更改 — 但是,完整的资源的表示将被发送到服务器.
然而,您可以覆盖 save 方法并通过选项提供 data
属性,该选项将发送到服务器而不是完整的模型表示.例如,这只会保存传递给方法的属性:
var M = Backbone.Model.extend({保存:功能(属性,选项){选项 ||(选项 = {});options.contentType = '应用程序/json';options.data = JSON.stringify(attrs);Backbone.Model.prototype.save.call(this, attrs, options);}});
还有一个小提琴 http://jsfiddle.net/dLFgD/
<小时>正如@mikebridge 在评论中指出的那样,现在可以通过传递 attrs
选项来获得此行为.所以要么使用
book.save(null, {属性:{作者:泰迪"}});
或保留覆盖
var M = Backbone.Model.extend({url: '/echo/json/',保存:功能(属性,选项){选项 ||(选项 = {});options.attrs = attrs;Backbone.Model.prototype.save.call(this, attrs, options);}});
http://jsfiddle.net/nikoshr/dLFgD/7/><小时>
如果您使用支持它的 Backbone 版本 (>=0.9.9) 并且您的服务器理解该动词,您也可以发送 PATCH 请求,如 在@pkyeck 的回答中解释
I'm using Backbone.js on the client and node.js on the backend, and I'm having a bit of trouble doing a 'limited' model save, as explained here : http://backbonejs.org/#Model-save
As in the example, if I do
book.save({author: "Teddy"});
how do I access the parameters of the save using express.js, i.e. the fact that I only want to save to the 'author' field? I've tried the following
req.body -> gives ALL parameters of the model being saved, I want only the 'author' field
req.query -> empty
Any help much appreciated!
As stated in Model.save documentation:
save model.save([attributes], [options])
[...] The attributes hash (as in set) should contain the attributes you'd like to change — keys that aren't mentioned won't be altered — but, a complete representation of the resource will be sent to the server.
You can however override the save method and provide a data
attribute via the options which will be sent to the server instead of the full model representation. For example, this will only save the attributes passed to the method :
var M = Backbone.Model.extend({
save: function (attrs, options) {
options || (options = {});
options.contentType = 'application/json';
options.data = JSON.stringify(attrs);
Backbone.Model.prototype.save.call(this, attrs, options);
}
});
And a Fiddle http://jsfiddle.net/dLFgD/
As @mikebridge noted in the comments, this behavior can now be obtained by passing an attrs
option. So either use
book.save(null, {
attrs: {author: "Teddy"}
});
or keep the override
var M = Backbone.Model.extend({
url: '/echo/json/',
save: function(attrs, options) {
options || (options = {});
options.attrs = attrs;
Backbone.Model.prototype.save.call(this, attrs, options);
}
});
http://jsfiddle.net/nikoshr/dLFgD/7/
You could also send a PATCH request if you're using a Backbone version that supports it (>=0.9.9) and your server understands that verb, as explained in @pkyeck's answer
这篇关于用于 model.save() 的 Backbone.js/express.js 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!