Question

I'm having trouble saving data in this model relationship. My models are as follows:

App.Flow = DS.Model.extend({
  title: DS.attr('string'),
  content: DS.attr('string'),
  isCustom: DS.attr('boolean'),
  params: DS.hasMany('parameter', {async: true})
});

App.Parameter = DS.Model.extend({
  flow: DS.belongsTo('flow'),
  param: DS.attr('string'),
  param_tooltip: DS.attr('string'),
  param_value: DS.attr('string')
});

As you can see, I want Flows to have multiple Parameters. I have a rudimentary setup using Flow and Parameter fixtures, which behave as expected in the templates. However, when I try to create new ones in the controller, I have trouble setting the flow and parameter values correctly.

var p = this.store.createRecord('parameter', {
  param: "foo",
  param_tooltip: "world",
  param_value: "hello"
});

var f = this.store.createRecord('flow', {
  title: 'job',
  content: title,
  isCustom: true,
  params: [p] // doesn't seem to work
});

f.set('params', [p]); // doesn't seem to work
p.set('flow', f); // also doesn't seem to work

// Save the new model
p.save();
f.save();

I've tried a lot of solutions after staring at this and StackOverflow for a while (not just the ones listed). I'm not really sure what to try next. One thing that I noticed in the Ember inspector was that the ids of these created elements were not integers (they were something like the string 'fixture_0'), but I'm not really sure why that would be, whether its related, or how to fix it.

Thanks!

Edit

After dealing with promises ala kingpin's solution, the parameter seems like it is being set in the createFlow function I have when I check with print statements; however, I'm still having trouble accessing the parameters in both my template and in the removeFlow function.

createJob: function() {
  // ...
  f.get('params').then(function(params) {
    params.pushObject(p);
  });

  f.get('params').then(function(params) {
    console.log(params.toArray()[0].get('param')); // prints 'foo'
  });
  // ...
},

removeJob: function(flow) {
  console.log(flow.get('title')); // prints 'job'
  flow.get('params').then(function(params) {
    var arr = params.toArray();
    console.log(arr); // prints '[]'
    for (var i=0; i < arr.length; i++) {
      console.log('hi'); // never gets printed
      arr[i].deleteRecord();
      arr[i].save();
    }
    flow.deleteRecord();
    flow.save();
  });
},
Was it helpful?

Solution

params being async needs to be waited on before you can set a property or model on it (this is something that is being hardened out before 1.0 finally hits, but it's finicky right now in 1.0 beta). The fixture_ids are applied if you're using the fixture adapter.

var store = this.store;
var p = store.createRecord('parameter', {
   param: "foo",
   param_tooltip: "world",
   param_value: "hello"
  });

 var f = this.store.createRecord('flow', {
   title: 'job',
   content: 'title',
   isCustom: true,
 });

f.get('params').then(function(params){
  params.pushObject(p);

});

 p.set('flow', f);

http://emberjs.jsbin.com/OxIDiVU/588/edit

Additionally saving using the fixture adapter seems to remove hasMany relationships when their is a manyToOne connection (aka both records have a relationship to each other, one of them being a hasMany). Getting rid of one of the relationships fixes the issue (or using one of the real adapters)

http://emberjs.jsbin.com/OxIDiVU/590/edit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top