这是我对这个问题的解决方案。我没有在骨干stickit config上做任何事情。如果用户单击取消按钮,我所做的是使用模型 ID 并从 restful 服务器获取原始数据。然后使用来自服务器的数据通过stickit 2方式绑定恢复模型更改。
canceledit: function() {
var modelIndex = this.model.get('index');
var modelId = this.model.get('id');
this.$el.fadeOut(500, function() {
var fetchTask = new App.Models.Task({ id: modelId });
fetchTask.fetch({
wait: true,
success: function(model, response, options) {
var title = model.get("title");
var task = App.Collections.tasksCollection.at(modelIndex);
task.set({title : title});
},
error: function(model, response, options) {
console.log('An error occured while fetching the data...');
}
});
this.remove();
});
}
如果您有不需要从服务器获取数据以通过主干网恢复模型更改的解决方案,请发布您的答案。stickit
更新 - 基于杰克建议的第二个解决方案 - 没有 REST 调用
//create global variable for model collection index and title properties
App.Global.modelTaskCurrentTitle = "";
App.Global.modelTaskIndex = -1;
//in edit view render function
//capture info needed
App.Global.modelTaskIndex = this.model.get('index');
App.Global.modelTaskCurrentTitle = this.model.get('title');
//in cancel function for edit-view
//or any view that need to remove edit-view to prevent zombie-edit-view
//and also reverting model changes by stickit in edit-view
//revert stickit changes
var task = App.Collections.tasksCollection.at(App.Global.modelTaskIndex);
task.set({title : App.Global.modelTaskCurrentTitle});
//remove edit view
App.Views.editTaskView.remove();