I had code that was working in earlier version of ember data(beta 3 and ember 1.5), but it is breaking in ember data ver 1.0.0 beta 15 and ember 1.10.0. I have a model that has parent/child relationship to itself.
App.Question = DS.Model.extend({
name: DS.attr('string'),
childQuestions: DS.hasMany('question', {async: true, inverse: 'parentQuestion'}),
parentQuestion: DS.belongsTo('question', {inverse: 'childQuestions'})
});
Now I want to add child question within a parent question in any position i want. I was able to add/delete question at any position before but now I am not able to do so. This is the core logic for adding model that I had:
addChild: function (parent, afterChild) {
var position = parent.get('childQuestions').indexOf(afterChild),
store = this.get('store'),
jqXHR = Ember.$.ajax({
url: '/questions',
dataType: 'json',
type: 'POST',
data: {
parentQuestion: parent.get('id')
}
});
jqXHR.done(function (data) {
Ember.run(function () {
// In later versions of Ember Data, this method returns the actual record
// that was created so we don't have to hunt for it again with `store.find()`.
store.pushPayload('question', data);
store.find('question', data.question.id).then(function (question) {
parent.get('childQuestions').insertAt(position + 1, question);
});
});
});
}
So now this is breaking and creates these 2 issues for me:
1) cannot move the newly added question in desired position
2) if i add some new question and then delete some of them and then add yet another new question, then it brings back all the deleted questions back. I checked in the store and no these records are no longer in the store!!
Here is a jsbin to demonstrate the issues I was having: http://ift.tt/1E6FKRI
I am assuming that I was using old version of both ember data and ember, I must be doing this the wrong(non ember) way, you insight on how to properly solve these will be much appreciated. thanks.
Aucun commentaire:
Enregistrer un commentaire