Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make _super() using the parent objects context?

Tags:

ember.js

App.CurtainView = Ember.View.extend
  name: "CurtainView"
  getName: ->
    console.log @get("name")

App.ButtonView = App.CurtainView.extend
  name: "ButtonView"
  getName: ->
    # Do something
    @_super()

Returns on console:

ButtonView

...but I expect:

CurtainView

I guess this is a context issue - how can I make _super() using the parent objects context?

like image 941
kraftwer1 Avatar asked Jun 11 '26 17:06

kraftwer1


1 Answers

I don't completely get your goal. This behavior is the same as you would expect with Ruby language, but indeed differs from what you would obtain with Java (assuming you would redefine the name attribute in Button subclass)...

In fact, you have to be aware of the way the Ember's object model is implemented to better understand the behavior you get:

App = Ember.Application.create();

App.CurtainView = Ember.View.extend({
  name: "CurtainView",
  getName: function() {
    console.log(this.get("name"));
  }
});

App.ButtonView = App.CurtainView.extend({
  name: "ButtonView",
  getName: function() {
    this._super();
  }
});

var c = App.CurtainView.create(),
    b = App.ButtonView.create();

console.log(c.getName.toString());
console.log(b.getName.toString());

c.getName();
b.getName();
b.getName.apply(c);

The _super call applies by default to the object on which the current function has been called.

You could override this default behavior by calling getName through apply or call, and passing the appropriated target. But it seems pretty weird to me...

JSFiddle here

like image 110
Mike Aski Avatar answered Jun 14 '26 10:06

Mike Aski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!