copenndthagen
copenndthagen

Reputation: 50742

Can "model" be treated as special controller property in EmberJS

I have 3 related questions;

Q1: In EmberJS, can "model" be treated as special controller property.

I am saying this, coz consider for a controller (has model returning firstName & lastName attributes), I have a property "isVisible"

So in the template, I would say

{{#if isVisible}}
Hello {{model.firstName}}, {{model.lastName}}
{{/if}}

Now here, "isVisible" is controller property and referred directly, whereas to display firstName/lastName, we say model.firstName & model.lastName

So that makes it seem like "model" is some kind of special property defined on the controller somewhat similar to "isVisible"

Q2: Also, I assume the model's value would always be returned by the corresponding route's model hook. Not sure if there are many other ways ?

Q3: Also is "model" a special/reserved keyword which Ember recognizes. I am asking this, coz I have observed

self.controllerFor('someroute').get('model.someParam')

It would be great if you could point the Ember library code for get/set where Ember handles this "model" keyword.

Upvotes: 1

Views: 35

Answers (1)

Bek
Bek

Reputation: 3207

A1:model property is not special property of controller, it might appear special because it is automatically/(behind the scenes) set by route in setupController() method, you can override it and use other property name for model instead

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

and in your template:

{{#if isVisible}}
Hello {{person.firstName}}, {{person.lastName}}
{{/if}}

now you have model set to property person instead of model in controller

A2: as model in controller is just property set by route, you can set/change it from anywhere in application (though that would be bad practise), right place to set models/data to controller is setupController() hook in route (shown above).

A3: model is not reserved keyword

Upvotes: 2

Related Questions