当我需要在模型中执行非平静操作时,如何传入数据?
例如......
module.exports = Base.extend({
url: {
'create': '/sites/:site_id/footer',
'update': '/sites/:site_id/footers/:activity',
'copy': '/sites/:site_id/:language/:label/footer/copy',
'delete': '/sites/:site_id/footer/:id'
},
api: 'service',
copy: function () {
var new_label = this.get( 'label' ) + ' Copy';
return this.save( {}, {
type: 'POST',
url: this._getUrl( 'copy' ),
data: JSON.stringify( {
action: 'copy',
label: new_label
} )
} );
}
});
问题是每当我们传入数据时......有效负载只会显示“动作”。标签完全被忽略。
答案 0 :(得分:0)
请改为尝试:
copy: function () {
var new_label = this.get( 'label' ) + ' Copy';
return this.sync(
'create',
this,
{
url: this._getUrl( 'copy' ),
attrs: {
action: 'copy',
label: new_label
}
}
);
}
这基本上是save
函数背后的魔力,没有所有自动收集要发送的数据。请注意,如果您在选项中使用attrs
键,那么您不需要自己stringify
数据,因为它将在同步方法中为您完成。