Hurelu
Hurelu

Reputation: 1538

module controller with initiation parameter

When using m.module, I often would like to provide arguments to the controller constructor so that the first rendering starts with the right data. However, the Mithril documentation and examples always show module.controller() and module.vm.init() without parameters.

To go around this issue and have module.controller(initData) I've resorted to use this small utility function to wrap and extend the existing m.Module:

var mModule = function (dom, mod, arg) {
  return m.module(dom, {
    view: mod.view,
    controller: mod.controller.bind(mod.controller,arg)
  });
};

Questions:

  1. Is this an anti-pattern? Is there an alternate recommended way instantiate the module with custom external data?
  2. Would this cause issues with m.route? I saw some mentions of recursive calls in the source code but could not get my head around it.
  3. Following the 2 points above, is the lack of parameter for m.module a deliberate design choice?

Oh...and thanks to all involved for the existing documentation and discussions.

Upvotes: 1

Views: 641

Answers (1)

LeoHorie
LeoHorie

Reputation: 1320

No, it's not an anti-pattern, and it's an idea that is explored in one of the blog articles, and also by Moria (a router extension library for Mithril)

Upvotes: 1

Related Questions