Sunday, 15 July 2012

meteor - Iron-Router RouteController Inheritance: Why does the Parent Controller's Hook run before the Child's? -



meteor - Iron-Router RouteController Inheritance: Why does the Parent Controller's Hook run before the Child's? -

i've been working fantastic iron-router bundle (0.7.1) meteor (0.8.1.3) , have run seems counter-intuitive. have provided illustration below.

the next code written in context of iron-router's provided tinytests. https://github.com/eventedmind/iron-router/blob/devel/test/both/route_controller.js

var parent = routecontroller.extend({ onbeforeaction: function(pause) { console.log('i\'m in parent!'); pause(); } }); var kid = parent.extend({ onbeforeaction: function(pause) { console.log('i\'m in child!'); pause(); } }); var inst = new child(router, route, {}); inst.runhooks('onbeforeaction');

the test resulted in kid printing out "i'm in parent" had expected kid print out "i'm in child"

i sense object oriented programming, more natural child's onbeforeaction override parent's.

that beingness said, if intentional, how can subvert order of hooks , have child's onbeforeaction run?

it looks intentional:

https://github.com/eventedmind/iron-router/blob/devel/lib/route_controller.js#l97

class="lang-js prettyprint-override">// concatenate hook arrays inheritance // heirarchy, starting @ top parent downwards child. var collectinheritedhooks = function (ctor) { var hooks = []; if (ctor.__super__) hooks = hooks.concat(collectinheritedhooks(ctor.__super__.constructor)); homecoming utils.hasownproperty(ctor.prototype, hookname) ? hooks.concat(ctor.prototype[hookname]) : hooks; };

if don't want parent hook run, looks you'll have skip using inheritance , mixin mutual functionality various controllers.

meteor iron-router

No comments:

Post a Comment