How to create and save a new model in Ember Data 1.13.7 and update the UI


#1

Hi.

I am using Ember 1.13.5 and Ember Data 1.13.7 and the default JSONAPIAdapter/Serializer and I have a problem saving a new model.

My route looks like this:

import Ember from 'ember';

export default Ember.Route.extend({

    model: function(params) {

	    return Ember.RSVP.hash({
		    auditLog: this.store.query('log', {filter: {object: 'IsoApplication', object_id: params.id}}),

	    });
    },
});

This successfully returns a collection of “log” models in the “auditLog” key of the controller, e.g. this.get(‘model.auditLog’).

I create a new “log” model and save it successfully like this:

self = this;

var log = this.store.createRecord('log', {
    logLevelId: 2
    logTypeId: 2
    object: 'IsoApplication',
    objectId: 23
	message: 'A message',
	logData: '',
});

log.save().then(function(log) {
    self.get('model.auditLog').pushObject(log);
});

I use this line:

self.get('model.auditLog').pushObject(log);

To update the model so that my new log entry is displayed in the UI but I get the following error:

TypeError: internalModel.getRecord is not a function

and the “log” parameter in the “save” callback is of type “InternalModel”. I was led to believe that InternalModels were only used by the Ember Data core code so presumably the “save” callback should return a DS.Model instance instead. Is something wrong here?

Also, am I doing the correct thing by using “pushObject” to update the “model.auditLog” collection of “log” models in order that the UI updates with the newly added model or should it be an automatic process?

Any help that anyone could offer would be appreciated.

Thanks.

I have also logged this issue on StackOverflow here:


#2

Ok, I have found the solution to this problem here:

solution

The specific post that helped me was:

var self = this;
var usersArray = [];
    store.find('user', params).then(function(users){
  usersArray.addObjects(users);
  usersArray.addObject(self.store.createRecord('user' ));
}

The pushObject() function can no longer be used with versions of Ember Data 19+.