I'm trying to use something like this:
var a = Marionette.Object.extend({
initialize: function () {
//Works fine
this.listenTo(this, "something", function(){...});
//Problem
(function(){
this.listenTo(this, "somethingElse", function(){...});
})();
//Same...
var b = function(){
this.listenTo(this, "somethingElse", function(){...});
}
b();
}
});
When I try to run this code, it says:
Cannot read property 'listenTo' of undefined
for the functions. But the question is why?
As far as I know, the initialization:
get's called, when everything is ready, that's why the other listener at the beginning works. If I put the listener into a function, it should be the same, because, when it gets called, the Object is already initialized so this
is present.
Any idea how could I solve this with, leaving the listener inside the function?
Thanks in advance!
Solution for the above (thanks to sp00m)
-- New question --
Achieving the same within an object:
var a = Marionette.Object.extend({
b: (function(){
this.listenTo(this, "somethingElse", function(){...});
})()
});