EventedMind/iron-router — pull request #283 by jagi (2013-11-07 22:16:14+00:00)
Title: Fixed error when collecting parent hooks

Child controller inherits prototype's methods from the parent controller prototype.

So, if we have `Parent` controller

```js
Parent.prototype.before = function () {
console.log('parent.before');
};
```

and we create `Child` class inheriting from the `Parent` we will have prototype as follows:

```js
Child.prototype.before = function () {
console.log('parent.before');
};
```

Now when collecting hooks, the result hooks array will contain both functions instead one and it will cause double hook execution.

The problem is this line: https://github.com/EventedMind/iron-router/blob/dev/lib/route_controller.js#L45

```js
return ctor.prototype[hookName] ? hooks.concat(ctor.prototype[hookName]) : hooks;
```

What it does is checking if given hook is in the given prototype but it will be true even if the hook is not in the prototype. It's because it will look in the prototype of the parent class and so on. So, if we have three classes inheriting from each other:

`C` <- `B` <- `A` <- `RouteController`

and only `A` class will have `after` or `before` hook, it will still think that each one has given hook.

To deal with that, we have to explicitly check if a given prototype has a hook by using `Utils.hasOwnProperty` function.

```js
return Utils.hasOwnProperty(ctor.prototype, hookName) ? hooks.concat(ctor.prototype[hookName]) : hooks;
```