Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the `this._super(controller,model)` mean in an Ember Router?

I have seen in EmberJS code and discussion {no references supplied} the following:

Code

route.js

setupController: function (controller, model) {
    this._super(controller,model);
    // More code
},

Questions

What is the call to this._super(controller,model); doing here?

When should I need to use this type of call?

Just trying to learn here as my nose bleeds from the Ember learning curve.

like image 812
datUser Avatar asked Jan 19 '15 16:01

datUser


People also ask

What is a controller in Ember?

What is a Controller? A Controller is routable object which receives a single property from the Route – model – which is the return value of the Route's model() method. The model is passed from the Route to the Controller by default using the setupController() function.

Why is ember not popular?

Ember is not suitable for smaller project since it can overcomplicate the app. Also, it is hard for new specialists to adopt this framework as the learning curve is steep.

What is the prime task performed by controllers in Ember JS?

What are the prime tasks that are performed by controllers in Ember. js? Decorating the model which is returned by the route is a very essential task that needs to be performed in Ember.


2 Answers

As @RyanHirsch said, this._super calls the parent implementation of the method.

In the case of setupController, calling this._super(controller,model) will set the 'model' property of the controller to the model passed in. This is the default implementation. Due to this, in normal situations we do not need to implement this method.

Now we normally override it when we want to set additional data to the controller. In those cases we want the default behavior and our custom stuff. So we call the _super method. And do our stuff after that.

setupController: function (controller, model) {
  // Call _super for default behavior
  this._super(controller, model);

  // Implement your custom setup after
  controller.set('showingPhotos', true);
}

Here is the default implementation of setupController.

like image 66
blessanm86 Avatar answered Oct 20 '22 01:10

blessanm86


this._super(controller, model); calls the parent implementation for the method (i.e. the object you are extending, so Ember.Route)

http://emberjs.com/guides/object-model/classes-and-instances/

"When defining a subclass, you can override methods but still access the implementation of your parent class by calling the special _super() method"

http://emberjs.com/guides/object-model/reopening-classes-and-instances/

like image 25
RyanHirsch Avatar answered Oct 20 '22 01:10

RyanHirsch