Ember.js: this._super in callback function

I am trying to figure out how to use this._super when an Ember object method is called from a callback.

I know that I could assign var _super = this._super before calling callback, but I don't like this.

I want to have this object containing the correct _super method inside the callback.

My code is here: http://emberjs.jsbin.com/hasehija/6/edit .

 App.BaseMixin = Ember.Mixin.create({ init: function() { console.log("base"); } }); App.Utils = Ember.Object.extend({ callbackMethod: function(callback, ctx) { // asynchronous callback Ember.run(function() { callback.call(ctx); }); } }); App.MyObject = Ember.Object.extend(App.BaseMixin, { init: function() { console.log("MyObject"); var _super = this._super; App.Utils.create().callbackMethod(function() { this._super(); // this._super is undefined here // _super() would work }, this); } }); App.ApplicationController = Ember.Controller.extend({ init: function() { new App.MyObject(); } }); 

Do you know how to fix this?


UPDATE:

It turned out that it was fixed in Ember 1.5.0 (@GJK: thanks for the answer), and I used Ember 1.4.0.

+2
source share
1 answer

extend defines a class

 App.Utils = Ember.Object.extend({ callbackMethod: function(callback, ctx) { callback.call(ctx); } }); 

create builds an instance of the class

 App.Utils = Ember.Object.create({ callbackMethod: function(callback, ctx) { callback.call(ctx); } }); 

or

 App.Utils.create().callbackMethod(function() { this._super(); }, this); 

http://emberjs.jsbin.com/hasehija/7/edit

Or avoid overriding init

 App.ApplicationController = Ember.Controller.extend({ doSomething: function() { new App.MyObject(); }.on('init') }); 
+2
source

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


All Articles