knockout.model icon indicating copy to clipboard operation
knockout.model copied to clipboard

afterHooks doesn't work with routeName as expected from example

Open neverfox opened this issue 12 years ago • 0 comments

In the example, you show urls being mapped to names like "show" and "create". You then demonstrate the use of __afterHooks by mapping a function to "create". So I would expect that when a call is made to create, that hook would fire, but it wouldn't because the following sequence of events takes place:

  • this.create calls this.doPost("create", params, callback) where "create" is mapped to the argument routeName
  • routeName, since it doesn't contain http, is used to get a url from __urls
  • This url is then passed to the static doPost mapped to its routeName argument, so where routeName was a string like "create" before, it's now a url
  • When the static doPost gets to the part where it checks ah[routeName], because routeName is now a url, it doesn't find a match. The "create" afterHook would never be fired, unless the original call to create was a static call, not an instance call. The same would go for any other hook that was registered to match a key in __urls.

I think the solution is as simple as removing

if (routeName.match(/^http:\/\//) === null) {
    url = this.__urls[routeName];
} else {
    url = routeName;
}

from the prototype version of doGet and doPost since it will always eventually call the static version, where the url will be looked up. I say I think it's that simple because I haven't thought about possible side effects.

neverfox avatar Jun 02 '13 09:06 neverfox