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

I'm new to ember and don't understand the difference between the two types of syntax. Where and in what situations should I use this or that. That is, which one is more suitable for use in routes, and which one is for controllers.

this.get('model') 

Unlike

 this.modelFor('artists/show') 
+6
source share
4 answers
 this.get('model') //controller call this.modelFor('someRoute') //route call 

In Ember, the default setupController route executes this one line of code:

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

This takes what is returned from the model hook and sets the controller model property with this value. Inside the controller, this.get('model') is the right way to access this model. In addition, the developer can override this hook and do something else, such as 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 a different route, you DO NOT get the controller model associated with the route that is installed in setupController . You get what returns from the model hook, which under the covers is the route currentModel property, if I remember correctly.

+5
source

this.get ('model') retrieves the model from the current area of ​​the controller.

this.modelFor ('artist / show') selects a model that will be in scope along the specified route.

+3
source

These two are defined for different ember objects - the model on the controller and the modelFor method on the route.

First of all, the model property and executing it as this.get('model') receives only the object that is already set in the model property in the controller. This means that it gets everything that was created during the setupController hook in the path. You cannot receive data from the API using this operator. In addition, it allows you to get only the object installed on the current controller ( this area).

When is it set up?

In setupController hook. The route life cycle first resolves all model-related beforeModel ( beforeModel , model and afterModel ). After that, it executes setupController with these arguments: (controller, model) . This means that it has access to everything that is allowed with the model hook and the current controller instance. By default, setupController sets the model on controller.model property:

 controller.set('model', model); 

You can override this with your custom code and, for example, set your model in the myModel property - it is up to you. But if you want to get your model in your controller code, you should always request the property on which you set your model.

How does #modelFor work?

modelFor defined in instances of routes and allows you to get the result of model binding from any route in your application. You can use it in your setupController hook as follows:

 controller.set('model', this.modelFor('myOtherRoute')); 

This behavior can be seen as an improvement in consistency in the application - it clearly says that in the current route and controller you use the same data as in myOtherRoute .

How it works? It requests the myOtherRoute.model property, which stores the model data of the allowed route. The implementation is quite simple - it looks for your container (a global object that stores your routes, controllers, components, etc.) for the route named myOtherRoute and gets its model property.

+3
source

this.get ('model') will be used in the controller in the computed property, say.

this.modelFor is the route for accessing the parent route model, to repeat or allow it.

+2
source

Source: https://habr.com/ru/post/987764/


All Articles