vendredi 4 septembre 2015

How do I create a new record with Ember Data and immediately add it to a hasMany attribute on another record?

Ember is fun but the documentation sure is thin.

I'm successfully displaying a hierarchy of Ember Data records, and now I want to allow the user to add new children (people) within the context of a parent (department).

I would have thought the code below would work, but it doesn't. While the newPerson is added to the data store, the person is not added to the department and I get errors in the console. Bonus for this question would be info on how to interpret Ember Data errors, as they're quite opaque. I can't even figure out which part of them to Google. Here I think the relevant part of the error is Promise.prototype._onError.

    addPerson(deptid) {
        var newID = Math.random().toString(16).slice(2);
        var newPerson = this.store.createRecord('person', {
            name: "Person Name",
            id: newID,
            devices: []
        }).save();

        this.store.find('department', deptid).then(function (department) {
            department.pushObject(newPerson);
        });
     }




Aucun commentaire:

Enregistrer un commentaire