Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js where to call this._super()

I've been going through the Ember documentation and am seeing an inconsistency in where the _super method is being called when overriding init.

This is the most common and is what I've been using so far

var Foo = Em.Object.extend({
    init: function(){
        this._super();
        // ... my stuff ...
    }
});

last night I was reading through this write up and saw an example doing this

var Bar = Em.Object.extend({
    init: function(){
        // ... my stuff ...
        return this._super();
    }
});

It was actually an Ember.ContainerView in the code snippet.

Can anyone explain this? My code OCD is acting up and I can't move on until I know.

like image 757
Ilia Choly Avatar asked Jun 01 '12 15:06

Ilia Choly


People also ask

How do I connect ember to backend?

You can use the --proxy option of ember-cli to proxy all requests made to your ember development server that are not handled by ember to your backend. This is a good way to do this because from the view of your application and browser your backend and your application are on the same host.

Is Ember JS still used?

Ember has been an enabler of great productivity for many teams for almost a decade and I'm sure it's going to continue to be that. It's changed and improved a lot since its first release and is now in better shape than ever with its Octane edition.

Is Ember hard to learn?

Ember is so hard to learn, I've been really struggling with it for the last six weeks. As regards getting the libraries to work, I hope this doesn't sound obvious but I recommend you download the ember starter kit from http://emberjs.com/ and start working from within that instead.

How does Ember JS work?

Ember uses templates to organize the layout of HTML in an application. Ember templates use the syntax of Handlebars templates. Anything that is valid Handlebars syntax is valid Ember syntax. Here, {{name}} is a property provided by the template's context.


1 Answers

In the documentation linked

 init: function() {
    var childViews = this.get('childViews');
    var descriptionView = App.DescriptionView.create();
    childViews.pushObject(descriptionView);
    this.addButton();
    return this._super();
  },

_super() is called AFTER the descriptionView is created and pushed onto the childViews array.

That's because the superclass init implementation is going to take the childViews array and do stuff with it. If you called _super before adding the descriptionView to the array, it wouldn't get processed by whatever init does....

I'm inferring, but that's the way it works in Sproutcore, from which Ember derives, so I think it's probably the same.

like image 123
hvgotcodes Avatar answered Oct 12 '22 23:10

hvgotcodes