azicchetti / jquerymobile-router

A router/controller for jquery mobile. Also adds support for client-side parameters in the hash part of the url. The routes handles regexp based routes. This plugin can be used alone or (better) with Backbone.js or Spine.js, because it's originally meant to replace their router with something integrated with jQM.
GNU General Public License v2.0
402 stars 69 forks source link

Execute handler functions with scope ControllerObject #22

Closed ivorbosloper closed 12 years ago

ivorbosloper commented 12 years ago

I would like handler functions to be executed in the ControllerObject scope (instead of the current no-scope). This allows you to use this in the ControllerObject/handler functions. Additionally, you could extend handlers with a scope keyword.

var router=new $.mobile.Router([
  { "#page": { handler: "page", events: "s" }},
  { "#test": { handler: "test", events: "s" }}],
  {
    sharedFunction: function(page) {
      // initialize footer on page
      $(page).append('<div>footer</div>');
    },
    page: function(eventType, matchObj, ui, page, evt) {
      this.sharedFunction(page);
      // other logic
    }
  }
]);
ivorbosloper commented 12 years ago

I'm trying to learn git/github, so let me know if I did something wrong...

azicchetti commented 12 years ago

Perfect, thanks for the patch.

I'll fix a small potential issue in a further commit, because the ControllerObject passed to the router is now just copied into an empty object {} [ cfr: $.extend(this.userHandlers,userHandlers||{}) ]. It's better to keep a reference to the original object, if provided by the user.