Order of executing event's callback

44 views
Skip to first unread message

Pera Jovic

unread,
May 8, 2015, 5:18:05 AM5/8/15
to twitter...@googlegroups.com
Hi, Flight people.
I have one question regarding to order of executing callbacks for some events.

I have two components -- first is A, and second is B. Both of them have this line in after initialize function:

//A.js
this.on("uiFoo", this.callback1);

//B.js
this.on("uiFoo", this.callback2);

When "uiFoo" is triggered, first A's callback is executed and then B's. The reason is that A's after initialize callback is called at the first place and after that B's. There are many more situation, but for sake of simplicity I'm giving this one. I want that B is executed first.

One work-around is:
//A.js
this.on("uiBar", this.callback1);

//B.js
this.on("uiFoo", this.callback2);
this.after("callback2", function (e) {
   
if (e.type === "uiFoo") {
       
this.trigger("uiBar");
    }
});


It seems clean (maybe), but there is a lot of boilerplate.

Is it possible somehow to control this order of execution or some better way to do this?

Regards,
Pera

Giuseppe Gurgone

unread,
May 8, 2015, 6:54:46 PM5/8/15
to twitter...@googlegroups.com
you clearly want A to do something when B is done something else, so your approach seems fine to me.

you could improve it as follow


//A.js
this.on("uiBar", this.callback1);

//B.js
this.onUiFoo = function (e, data) {
   this.callback2(e, data);
   this.trigger("uiBar", data);
};
this.on("uiFoo", this.onUiFoo);

Or (gross and fragile)

B.attachTo() first
and then
A.attachTo()

ie. invert the order
Reply all
Reply to author
Forward
0 new messages