Ember Data: child records disappear from screen after one flash when created

老子叫甜甜 提交于 2019-12-11 08:42:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!