PDA

View Full Version : [WISHLIST] Controllers control{} that can handle application events



estesbubba
27 Jan 2012, 2:30 PM
In our 2 current Touch 2 applications we have 2 kinds of events:
- events that views fire
- events that controllers fire

It appears that control{} can only be used to listen to view events. We've tried and haven't been able to listen to controller events, so our code looks like



config: {
control: {
'main #nextButton': {
tap: 'onNextButtonTap'
},


'main #backButton': {
tap: 'onBackButtonTap'
}


},


refs: {
main: 'main',
entryForm: 'entryform',
scanBarCode: 'scanbarcode'
}
},


init: function() {
var me = this;


me.getApplication().on({
startAccountLookup: me.startAccountLookup,
barCodeReaderEvent: me.barCodeReaderEvent,
scope: me
});


},


What would be nice is for control to be able to handle both events (maybe it can and we're just missing it).It seems several people have been posting on the 2.0 Discussion forum confused how to handle non-view events.

How about something like an 'application' selector so all events can go under control{}?



config: {
control: {
// view events
'main #nextButton': {
tap: 'onNextButtonTap'
},


'main #backButton': {
tap: 'onBackButtonTap'
},

// controller events
'application': {
startAccountLookup: 'startAccountLookup',
barCodeReaderEvent: 'barCodeReaderEvent'


},


refs: {
main: 'main',
entryForm: 'entryform',
scanBarCode: 'scanbarcode'
}
}

edspencer
27 Jan 2012, 3:17 PM
I've moved this to Discussion as it's not a bug... I'm not sure this is such a great path to start down as it dilutes the purity of control meaning CQ selection-based events and opens up the expectation that we'll therefore support model events, store events and anything else that may throw events, all from the control block.

I'd rather we have a convention around attaching events listeners to the Application just like any other observable:



init: function() {
this.getApplication().on({
someEvent: this.takeSomeAction
});
}

estesbubba
27 Jan 2012, 3:36 PM
Ed thanks for explaining. We are doing application events exactly how you posted. I think some people are confused how to handle non-view events and hopefully this clears it up.

edspencer
27 Jan 2012, 4:07 PM
I'll add this to the docs :)