问题
I am trying to update the arraycontroller model after deleting a record using jquery ajax.I can add a new object using self.pushObject(data)
, but i cannot remove the object using self.removeObject(data)
. can someone please help. ("data" is the object that i removed, the server sends it back after removing it in server.)
removeTodo: function(id) {
var page_id = id;
self = this;
Ember.$.ajax({
url: url+id,
type: "DELETE"
}).then(function(data) {
self.removeObject(data);
});
}
回答1:
data
might have the same properties, but it isn't the object that exists in your array. See here, both of these objects look exactly alike, but they are different objects, and as such aren't equal.
{ foo : 7 } != { foo : 7 }
When removing from a collection, if you pass in the object to be removed, that object must exist in the collection.
You would want to first find the object, then remove it from the collection.
.then(function(data) {
var obj = self.findBy('id', id); // assuming the object has a property 'id'
self.removeObject(obj);
});
来源:https://stackoverflow.com/questions/27454228/ember-js-how-to-remove-object-from-array-controller-without-ember-data