0
votes

Using emberjs 1.5.1 and ember-data 1.0.0-beta.5. I have couple of models:

App.Queue = DS.Model.extend({
    queue_id:   attr('number', {}),
    services:   hasMany('service', {})
});

App.Service = DS.Model.extend({
    queue:      belongsTo('queue', {}),
    service_id: attr('number', {}),
});

Here I create queue instance, save it, then try to create service instance and put created queue in belongsTo field

var new_queue = controller.store.createRecord('queue', {queue_id: element.Item1.Id});
new_queue.save();

var new_service = controller.store.createRecord('service', {
                                    queue: new_queue,
                                    service_id: subelement.Item1.Id,
                                });
new_service.save();

and after this I get new_service instance with the queue field equals undefined. Any ideas appreciated

1

1 Answers

0
votes

Considering this is a very old and no longer supported release of the framework, please take this answer as a best guess, based on my experience with Ember.js back then and the docs available for 1.10.0

Following the docs, the result of createRecord can't be used directly. By using the promise returned from the save function and wrapping everything in a .then callback, you should be able to achieve what you're looking for.

var new_queue = controller.store.createRecord('queue', {
    queue_id: element.Item1.Id
});

new_queue.save().then(function(new_queue_record) {
    var new_service = controller.store.createRecord('service', {
        queue: new_queue_record,
        service_id: subelement.Item1.Id,
    });

    new_service.save();
});