我正在开发一个项目,其中请求和响应有效负载都是命名空间。例如:
{
'SourceMachine':{
'Host':'some value',
'Description':'some value',
'UserName':'some value',
'Password':'some value'
}
}
为了能够在各个字段上执行get()和set(),我覆盖了我的“source_model”中的parse方法,如下所示:
parse : function(response, xhr) {
return response.SourceMachine;
}
这一切都很好。但问题是:当我想进行POST或PUT操作时,我必须将Host,Description,UserName和Password属性放入SourceMachine命名空间。基本上我一直在做的是将模型属性复制到临时对象,清除模型,然后保存如下:
var tempAttributes = this.model.attributes;
this.model.clear();
this.model.save({SourceMachine: tempAttributes});
这很有效,但它却是KLUGE的尖叫声!必须有更好的方法来处理命名空间数据。谢谢!
更新
我现在为基础模型创建了一个require.js模块,我将用于我应用中的所有模型,因为它们都依赖于命名空间数据:
define(function() {
return Backbone.Model.extend({
/**
* Adding namespace checking at the constructor level as opposed to
* initialize so that subclasses needn't invoke the upstream initialize.
* @param attributes
* @param options
*/
constructor : function(attributes, options) {
//Need to account for when a model is instantiated with
//no attributes. In this case, we have to take namespace from
//attributes.
this._namespace = options.namespace || attributes.namespace;
//invoke the default constructor so the model goes through
//its normal Backbone setup and initialize() is invoked as normal.
Backbone.Model.apply(this, arguments);
},
/**
* This parse override checks to see if a namespace was provided, and if so,
* it will strip it out and return response[this._namespace
* @param response
* @param xhr
* @return {*}
*/
parse : function(response, xhr) {
//If a namespace is defined you have to make sure that
//it exists in the response; otherwise, an error will be
//thrown.
return (this._namespace && response[this._namespace]) ? response[this._namespace]
: response;
},
/**
* In overriding toJSON, we check to see if a namespace was defined. If so,
* then create a namespace node and assign the attributes to it. Otherwise,
* just call the "super" toJSON.
* @return {Object}
*/
toJSON : function() {
var respObj = {};
var attr = Backbone.Model.prototype.toJSON.apply(this);
if (this._namespace) {
respObj[this._namespace] = attr;
} else {
respObj = attr;
}
return respObj;
}
})
});
答案 0 :(得分:2)
在模型上调用toJSON
创建和更新操作以生成服务器的数据:
toJSON
model.toJSON()
返回模型的属性的副本以用于JSON字符串化。在切换到视图之前,这可用于持久性,序列化或扩充。
您可以提供自己的toJSON
:
toJSON: function() {
return { SourceMachine: _(this.attributes).clone() };
}
这应该让你的服务器满意。遗憾的是,toJSON
也常用于为模板提供数据,您可能不希望其中出现SourceMachine
噪音;如果是这样,那么添加另一种方法来为模板准备数据:
// Or whatever you want to call it...
valuesForTemplate: function() {
return _(this.attributes).clone();
}
这就是标准toJSON
方法在内部的作用。