1
votes

I have a simple model named "test" that looks like this:

export default DS.Model.extend ({
  name: DS.attr('string')
  })

My model hook in the "test" route is defined as follows:

 export default Ember.Route.extend({
   model() {
     return this.store.createRecord('test');
     }
 });

I tried to assign an initial value to model.name in the "test" controller, but I can't get the 'set' to work.

export default Ember.Controller.extend({
  init: function() {
    let mod = this.get('model');
    this.set (mod.name, "dummyName");
    }
})

Ember inspector says mod=null. What am I doing wrong?

Update: I tried the 4 syntax changes as recommended in the comments below. None seem to work. I get the following error messages for each:

None of the 4 suggested syntaxes seem to work. I get the following error messages for each:

  1. Assertion Failed: Path 'model.name' must be global if no obj is given. Error: Assertion Failed: Path 'model.name' must be global if no obj is given.
  2. You need to provide an object and key to `set`. Error: Assertion Failed: You need to provide an object and key to `set`.
  3. Cannot read property 'set' of null TypeError: Cannot read property 'set' of null
  4. object in path "model" could not be found or was destroyed. Error: Property set failed: object in path "model" could not be found or was destroyed.

Seems to be telling me that 'model' is null for some reason.

Update:My controller actually looks like this:

import Ember from 'ember';

export default Ember.Controller.extend({

  init: function() {
    this.set ('model.name', 'dummyName');
  },
  actions: {
    save() {
      this.get('model').save();
      this.transitionToRoute('scenarios');
      return false;
    },
    cancel(){
      this.transitionToRoute('scenarios');
      return false;
    }
  }
});

With this controller, I get this error:

Property set failed: object in path "model" could not be found or was destroyed. Error: Property set failed: object in path "model" could not be found or was destroyed.

If I took out the 'init' hook, the 'save' & 'cancel' actions works fine. Naming seems to be ok; I used ember-cli pods to generate the initial code

Is 'model' loaded at the time when the controller's 'init' hook is called?
2
Can you recreate your problem in ember-twiddle.com or put it up in a public repo?Patsy Issa

2 Answers

1
votes

Try adding this to your route.js file:

setupController(controller, model) {
  controller.set("model", model);
}
0
votes

It is likely that the model is not loaded in the init hook. You could wrap your code in a run later:

init: function() {
  this._super();
  var _this = this;
  Ember.run.later(function() {
    _this.set('model.name', 'dummyName');
  });
},

Or a better way would be to set the property when creating the model:

return this.store.createRecord({name: 'test'});

as suggested in the comments.