wallop wallop - 7 months ago 12
Javascript Question

Ember.run.bind is not working

I am trying to get ember.run.bind to work but it just doesn't seem to work, any idea? I have tried all combinations

_didInsertElement: Ember.on('didInsertElement', function () {
Ember.run.bind(this, this.doSomething);
})


or

_didInsertElement: Ember.on('didInsertElement', function () {
Ember.run.bind(this, function() {
this.doSomething();
});
})


or

_didInsertElement: Ember.on('didInsertElement', function () {
var _this = this;
Ember.run.bind(this, function() {
_this.doSomething();
});
})

Answer

Ember.run.bind() returns a function that you can then call. It's meant for some asynchronous execution, so it doesn't expect to be called immediately, in the case of calling it immediately, it's unlikely you would need to use bind.

var func = Ember.run.bind(this, this.doSomething);

func();

http://emberjs.jsbin.com/diqelezika/edit?html,js,output