Javascript 在RESTFul应用程序中使用Backbone.js克隆资源的最佳实践是什么?
我正试图找出一种最好的方法,在这个例子中,我需要使用Backbone.js和RESTful架构克隆一个资源请求 当用户克隆某些资源时,应出现一个对话框,在该对话框中他可以为请求键入新名称并设置新的死线日期,所有其他字段必须从其他请求中克隆。对话框上显示的名称应类似于请求名称克隆1,其中的数字应为已创建的副本数 下面的代码表示了我是如何设计它的:Javascript 在RESTFul应用程序中使用Backbone.js克隆资源的最佳实践是什么?,javascript,rest,backbone.js,Javascript,Rest,Backbone.js,我正试图找出一种最好的方法,在这个例子中,我需要使用Backbone.js和RESTful架构克隆一个资源请求 当用户克隆某些资源时,应出现一个对话框,在该对话框中他可以为请求键入新名称并设置新的死线日期,所有其他字段必须从其他请求中克隆。对话框上显示的名称应类似于请求名称克隆1,其中的数字应为已创建的副本数 下面的代码表示了我是如何设计它的: var RequestModel = Backbone.Model.extend({ url: '/path/to/request',
var RequestModel = Backbone.Model.extend({
url: '/path/to/request',
idAttribute: "Id",
defaults: {
Name: '',
DeadlineDate: '',
// loads of other options
Content: '',
Documents: []
TeamMembers: []
}
})
// Request View
var RequestView = Backbone.View.extend({
events: {
'click .btn-clone': 'clone'
},
clone: function(e){
var id = $(e.target).closest('.request').data('request-id'),
referenceModel = new RequestModel({ Id: id });
// with the clone true param the server should append
// to the Name of the Request: (Clone *Number*)
referenceModel
.on({ 'sync': this.renderCloneDialog }, this)
.fetch({ data: { clone: true } });
},
renderCloneDialog: function(model){
// create a brand new instance of the model
// with the previous values
var clonedRequest = model.toJSON();
clonedModel = new RequestModel( _.omit(clonedRequest, 'Id') );
// send the cloned from id so the server use as reference
clonedModel.set('ClonedFromRequestId', clonedRequest.get('Id'));
var clonedRequestDialog = new CloneRequestDialogView({
model: clonedModel
});
}
});
// Request View
var CloneRequestDialogView = Backbone.View.extend({
// the method for saving will be just saving
saveClone: function(){
// update data based on the form
// and save the model
this.model.save();
}
});
以下是有关克隆工作原理的工作流:
用户单击克隆。
请求获取url参数为clone:true的资源。这将返回附加克隆“Clone number”的请求名称。
将打开一个对话框,用户可以在其中输入一些数据。
将创建一个没有Id的新模型,并在父请求中设置一个新属性。例:ClonedFromRequestId:123。
此新模型已保存。
我不知道这是否是这种情况下的最佳实践