Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding handlers and events to router #56

Open
satyasuman opened this issue Aug 22, 2012 · 2 comments
Open

Adding handlers and events to router #56

satyasuman opened this issue Aug 22, 2012 · 2 comments

Comments

@satyasuman
Copy link

Hi, can we add the events and the handler objects like the below

var eventsAndMethods = {};
eventsAndMethods.test = {};
eventsAndMethods.events = "bs";
eventsAndMethods.handler = "showIndexPage"; // This method will be available in the handlerObj

jqmRouterInstance.add([eventsAndMethods], handlerObj);

@satyasuman
Copy link
Author

This is in continuation to the above question.I am trying to implement a wrapper over the jqmRouter for my application

var baseRouter =  (function(){
    var routeControllerObjStore = [];
    var jqmRouterInitialized = false;
    var registeredBeforeInitialize = false;
    var baseRouter = {
        version: "0.1",
        registerRouteController: function (routeControllerObj, handlerEventObjsArray) {
            registeredBeforeInitialize = !jqmRouterInitialized;
            var handlerEventArray = new RouterHelper(handlerEventObjsArray).processEventsObj();
            routeControllerObjStore.push({"controller":routeControllerObj, "handlerEventArray":handlerEventArray});
            if(jqmRouterInitialized)
                this.addToRouter(handlerEventArray, routeControllerObj);
        },
        addToRouter: function(handlerEventArray, routeControllerObj){
            this.jqmMobileRouter.add(handlerEventArray, routeControllerObj);
        },
        initializeRouter: function(){
            this.jqmMobileRouter = new $.mobile.Router();
            jqmRouterInitialized = true;
            var tempStore = routeControllerObjStore;
            routeControllerObjStore = [];
            if(registeredBeforeInitialize){
                $.each(tempStore, function(index, controllerObj){
                    baseRouter.addToRouter(controllerObj.handlerEventArray, controllerObj.controller);
                })
            }
        }
    }
    var RouterHelper = function(handlerEventObjsArray){
        this.processEventsObj = function(){
            var jqmRouteControllerObj = {};
            $.each(handlerEventObjsArray, function(index, handlerEventObj){
                jqmRouteControllerObj[handlerEventObj.url] = {handler:handlerEventObj.handler, events: handlerEventObj.events.join(",")};
            });
            return [jqmRouteControllerObj];
        }
    }
    return baseRouter;
});

RouteController = RouteController.extend({
name: "MBARouteController",
initialize: function(){
console.log("Initializing the MBA app route controller");
},
showIndexPage: function(){
console.log("showing the index page");
}
});
baseRouter.registerRouteController(new RouteController(), [{"url":"Test.html","handler":"showIndexPage","events":[Router.Events.PAGE_BEFORE_SHOW]}]);

However the event doesn't seem to be firing and there is no error on my console too. How can i check if the event has been added to JQM router with out any issue.

@azicchetti
Copy link
Owner

Hi,
the right syntax should be (I've changed the example a little):

var eventAndMethod = {}, eventAndMethod2 = {};
eventAndMethod["/test.html"] = {};
eventAndMethod["/test.html"].events = "bs";
eventAndMethod["/test.html"].handler = "showIndexPage";
eventAndMethod2["/test2.html"] = {};
eventAndMethod2["/test2.html"].events = "bs";
eventAndMethod2["/test2.html"].handler = "showIndexPage2";

jqmRouterInstance.add([eventAndMethod, eventAndMethod2], handlerObj);

If you still have problems with your code, please write again and I'll dig some more into your code.

Cheers,
Andrea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants