我实际上是使用Ajax而不是Ember-Data在我的Ember应用程序中进行CRUD操作。
场景是每当我删除记录时,模型都不会更新。我有一个项目列表,每个项目前面都有一个“删除”按钮。
以下是与删除相关的操作:
deleteItem: function(item){ //function for delete action
var id = item.get('id');
$.ajax({
type : "POST",
url : "http://pioneerdev.us/users/deleteProject/" + id,
dataType : "json",
success : function(data) {
console.log('success');
}
});
alertify.success('Record Deleted!');
window.setTimeout(function(){location.reload()},3000)
}
如您所见,我正在使用location.reload
手动重新加载模型。如果有兴趣的话,他可以查看我的GitHub repo
有没有更好的方法呢?
答案 0 :(得分:1)
我用一些评论更新了你的代码。我希望它有所帮助。
actions: {
deleteItem: function(item){
var id = item.get('id');
var controller = this;
$.ajax({
type : "POST",
url : "http://pioneerdev.us/users/deleteProject/" + id,
dataType : "json",
// use the success callback to safe notify the user when the record is deleted
// in your current implementation the alert is displayed even if an error occurs in the server.
success : function(data) {
// no need to location.reload, the removeObject will remove that item
controller.removeObject(item);
alertify.success('Record Deleted!');
}
});
}
},
filteredContent : function() {
var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');
return this.get('arrangedContent').filter(function(item) {
return regex.test(item.projectname);
});
// instead of model use model.length so your template will update when some item is added or removed
}.property('searchText', 'model.length')