Reputation: 21
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:
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?Upvotes: 1
Views: 2821
Reputation: 4114
Try adding this to your route.js
file:
setupController(controller, model) {
controller.set("model", model);
}
Upvotes: 1
Reputation: 111
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.
Upvotes: 0