Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access ember model in on("init") in object controller

Tags:

ember.js

How to access ember model in on("init") in object controller? Its undefined in this case:

  inititalise: function () {

    // this.model is undefined

  }.on("init"),
like image 218
FutuToad Avatar asked Dec 06 '14 14:12

FutuToad


People also ask

How do you create an ember object?

To define a new Ember class, call the extend() method on EmberObject : import EmberObject from '@ember/object'; const Person = EmberObject. extend({ say(thing) { alert(thing); } }); This defines a new Person class with a say() method.

What is model in Ember JS?

In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.

What is controller in Ember JS?

In Ember. js, controllers allow you to decorate your models with display logic. In general, your models will have properties that are saved to the server, while controllers will have properties that your app does not need to save to the server.

How do I create a class in Ember?

To define a new Ember class, call the extend() method on Ember. Object : App. Person = Ember.


1 Answers

The short answer is that you can't. Controllers are instantiated by the container before the route has had a chance to set the model on the controller. If this object controller is a controller set up automatically for you by Ember, you're not going to be able to access the model during initialization. Try observing the model property instead.

I can't really find any documentation in the guides on this, so I'll link you to the source code. If you read the source for the setup function, you'll see that generateController() is called and then setupController() is called some time later. Essentially, the controller is created, Ember does some work, then the model is set. Ember doesn't set the model for the controller on creation.

like image 57
GJK Avatar answered Nov 04 '22 15:11

GJK