问题
I have a Todo-App with a list of checkboxes after each task.
I'm using a hasmany relation between task and checkboxes.
Todos.Todo = DS.Model.extend({
title: DS.attr('string'),
quarters: DS.hasMany('quarter',{async:true})
});
Todos.Quarter = DS.Model.extend({
filled: DS.attr('boolean'),
todo: DS.belongsTo('todo')
});
When I create a new task with new child records the new checkboxes flash one time on the screen and then disappear. This happens when I'm using the FixtureAdapter. Here's a JsFiddle with that behaviour: http://jsfiddle.net/Ld7gf/9/
// action in controller
createTodo: function(){
var title = this.get('newTitle');
if (!title.trim()) {
return;
}
var todo = this.store.createRecord('todo', { title: title });
for(var i = 0; i < this.hours; i++){
//Create a new child record
var quarter = this.store.createRecord('quarter', { filled: true });
//Save the child and then the parent
quarter.save().then(
function () {
//Succesful save of child; thus add to parent
todo.get('quarters').pushObject(quarter);
todo.save();
})
}
}
When I'm using LSAdapter (which I actually want) it throws
in Chrome while page is loading: Uncaught TypeError: Cannot read property 'add' of undefined
in Firefox when adding a task: Error: Assertion Failed: The content property of DS.PromiseArray should be set before modifying it
Any help, or links with good examples would be much appreciated.
回答1:
since quarters
is an async property you need to wait for it to load before attempting to use it (regardless of whether or not it's empty).
function () {
//Succesful save of child; thus add to parent
todo.get('quarters').then(function(quarters){
quarters.pushObject(quarter);
todo.save();
});
})
The documentation is still in a haze while they hammer out version 1.0 of Ember Data, until then the transition document is the go-to for documentation.
https://github.com/emberjs/data/blob/master/TRANSITION.md
来源:https://stackoverflow.com/questions/23475199/ember-data-child-records-disappear-from-screen-after-one-flash-when-created