jsdatajs-data-angular

How to push to an array in a js-data resource


I have a User object which has many route objects.

I load them like:

return User.find(username).then(function(user) {
    return User.loadRelations(user.username, ['routes'])
        .then(function(user) {
            return user;
        });
});

My user object looks like: this

I want to be able to go user.routes.push(newRoute) and then save the user. But I'm unable to push a new route.


Solution

  • 1) js-data <= 2.x does not support deep saving of relations. You would need to add a method to your Resource that sends the nested payload to the adapter.

    2) Also, by default, fields specified by the localField option in relation configurations will be a property accessor that dynamically pulls the related items from the store. It doesn't make any sense to push onto the array returned by the property accessor, because it's a different array every time you access the property.

    You can remove the property accessor with the linkRelations: false setting, but then you have to manage the links manually. In that scenario you can push onto the array, but that doesn't help with 1) above.