Are DS.RecordArrays now readonly?


#1

Hi.

I am using Ember 1.13.5 and Ember Data 1.13.7 and I am really struggling to understand how to update my template with a newly saved record.

Initially, my route loads all of the “log” models in to a recordarray stored in the “model.auditLog” controller property using the following code:

export default Ember.Route.extend({
    model: function(params) {
        return Ember.RSVP.hash({
            auditLog: this.store.query('log', {filter: {object: 'someObject', object_id: params.id}}),
            otherData: this.store.findRecord('otherData', params.id)			
        });
    }
});

I then have an “auditLog” computed property that depends on “model.auditLog” and changes it in to JSON structured in a specific way to load in to a table component in the template.

I create a new “log” model in the store and save it using the following code:

var log = this.store.createRecord('log', {
    logLevelId: 2,
    logTypeId: 2,
    object: 'myObject',
    objectId: this.get('application.id'),
    message: 'my message text'
    logData: '',
});

log.save().then(function(log) {
    /* I want to update my model here so that the newly added log object shows in my template */
}

However, the template is not updated with the new “log” model even though it is in the Store as it shows up in the Ember Inspector. The reason for this is that the “model.auditLog” is not being updated and as a result the “auditLog” computed property is not being fired off.

What is the correct way to get my template to update? Do I have to re-run the route to refresh the model as that seems overkill? Or, do I have to create a new array from the “model.auditLog” and then do an “addObject(newLogModel)” on that? I can’t get any of these approaches to work at present and my model and thus my template is never updated.

I’m sure there must be a simple way of doing this as it is a common occurrence so if anyone can help me I’d be grateful.

Thanks.


#2

I’ve found a solution that works though I’d still be interested to know if it’s best practice. Just call the same query that I did originally in the route but in the success callback of the .save() method like this:

self.store.query('log', {filter: {object: 'myObject', object_id: self.get('application.id')}}).then(function(logs) {
    self.set('model.auditLog', logs);
});

The “model.auditLog” property is set when the promise resolves and the template is updated. I was thinking that perhaps the calls to the store should only be made in the route but I guess I was mistaken because doing it this way in the controller is simple and effective.