9

When using the [relatively new] MVC bits in Sencha Touch I find that 90% of my dispatch calls look something like:

Ext.dispatch({
    controller: "customers",
    action: 'show',
    record: record
});

That's fine, and I like delegating the flow to the seperate controller code, rather than complicated paths of events, but I have yet to see any advantage over just doing something like:

controllers.customers.show({
    record: record
});

Which is shorter and cleaner. I feel like I am just following the Ext.dispatch pattern for the sake of it. My app doesn't benefit from push/pop url history for state, and that is the only reason I can see for using this longer more complex approach.

What am I missing? What do I gain from using Ext.dispatch to call controllers?

Chris Farmiloe
  • 13,935
  • 5
  • 48
  • 57

1 Answers1

9

The beforedispatch event is handy in case you need to redirect them to another controller.

Using dispatch also lets me load the controller code as needed instead of all at once on page load. I cut my app's startup time in half that way.

You said your app didn't need it, but being able to set the historyUrl and directly link to pages would be the main benefit for other users I think.

So I guess it all depends on the app whether it makes sense to use it.

Jason Freitas
  • 1,587
  • 10
  • 18
  • can you elaborate more on how you load the controller code JIT, or link to a tutorial about this? – Tomáš Fejfar Dec 01 '11 at 13:35
  • @TomášFejfar I save the Ext.util.Dispatcher dispatch function to a closure variable, then I replace dispatch with my own code. Inside the new function I look at the options that were passed to dispatch which should contain the name of the controller. If Ext.ControllerManager.get returns a controller with that name, I call the original dispatch function. Else I load the script containing the controller (i.e. "/app/controllers/users") dynamically with requireJS (any similar library will do). When the script is done loading, my controller should be registered and I call dispatch as usual. – Jason Freitas Dec 02 '11 at 00:13
  • BTW this was done for 1.0. Sencha touch 2.0 might make my method obsolete. – Jason Freitas Dec 02 '11 at 00:17