4
votes

Hi and thanks for taking the time to answer my question.

I have the following code in my controller:

.
.
.

var project = this.store.createRecord('project', {
                name: projectName,
            });

            project.save();
            alert(project.get('id') + project.get('name'));
.
.
.

This adds the record successfully to the db and the front end is automatically refreshed to reflect these changes. When the alert is executed however, only the name is printed.

For the ID i get null.

Basically I need to get the id value returned from the server after the record has been successfully isnerted. I checked the returned JSON:

{"project":{"id":6,"name":"dfgdgf", "created_at":"2014-01-02T08:37:57.491Z","updated_at":"2014-01-02T08:37:57.491Z"}}

so the id field is contained in the json response. Could the problem occur because the server also returns two fields (created_at and updated_at) which are not found in the ember model??

Thank you so much!

EDIT:

I included created_at and updated_at in my model in ember and am still getting null when I try to call project.get('id');

Any suggestions?

Thanks

3
the data from the server are returned asynchronously, so you actually need to use a callback function and call alert, or use the returned data in that function.melc
@melc, could you point me to a link that would give me some guidance?Dragan
sure, possible solutions can be found in these threads, stackoverflow.com/questions/19574133/… and stackoverflow.com/questions/17790671/…melc
melc, you can post your comment as an answer and I will mark it as correct. Thanks! Did not know that it is a known bugDragan

3 Answers

2
votes

The data from the server are returned asynchronously, so to is required to use a callback function and call alert, or use the returned data in that function.

Possible solutions can be found in the following threads,

is handling custom server side errors in ember-data when saving model possible

Get Ember-Data REST response value

2
votes
var project = this.store.createRecord('project', {
    name: projectName,
});

project.save().then(function(projectResponse) {
    alert(projectResponse.get('id') + projectResponse.get('name'));
});

This would work for sure !! :)

0
votes
 savePerson: function(hash){
     return this.store.createRecord('agents/person', hash).save();
    },


  var hash = {
                      given_name : firstname,

         };

  this.savePerson(hash).then(function(record){
                var personModelId = record.get('id');

I got mine like That