Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between this.get('model') and modelFor

I am quite new to ember and don't really get the difference between two types of syntax. Where and in which situations should i use one or another. I.e. Which one is more suitable for usage in Routes and which one for Controllers.

this.get('model')

As opposed to

this.modelFor('artists/show')
like image 216
canufeel Avatar asked May 22 '15 17:05

canufeel


People also ask

What is setupController in Ember?

By default, setupController will set the model property of controller to the value returned from model hook of the route. You could also you it to, for example, set the model of another controller, or set some initial controller state that depends on the model.

What is computed in Ember?

What are Computed Properties? In a nutshell, computed properties let you declare functions as properties. You create one by defining a computed property as a function, which Ember will automatically call when you ask for the property. You can then use it the same way you would any normal, static property.

How do I use Ember data?

It is the main interface you use to interact with Ember Data. In this case, call the findAll function on the store and provide it with the name of your newly created rental model class. import Route from '@ember/routing/route'; export default Route. extend({ model() { return this.


2 Answers

this.get('model') fetches the model from the current controller scope.

this.modelFor('artists/show') fetches the model that would be in scope at the specified route.

like image 110
Christopher Milne Avatar answered Sep 17 '22 15:09

Christopher Milne


this.get('model') //controller call
this.modelFor('someRoute') //route call

In Ember, a routes setupController hook by default performs this one line of code:

setupController: function(controller, model){
   controller.set('model', model);
}

This takes whatever is returned from the model hook and sets the controller's model property with this value. From within the controller, this.get('model') is the proper way to access this model. Also, a developer can override this hook and do something different, like set model equal to some child property of what is returned from the model hook (controller.set('model', model.prop). This is worth noting, because when you call this.modelFor from another route, you DO NOT get the route's associated controller's model that is set by setupController. You get whatever is returned from the model hook, which under the covers is the route's currentModel property if I remember correctly.

like image 28
mistahenry Avatar answered Sep 20 '22 15:09

mistahenry