简体   繁体   English

具有客户端路由的PhoneGap / Cordova?

[英]PhoneGap/Cordova with client-side routing?

I've inherited a Cordova/PhoneGap app running Cordova 3.4. 我继承了运行Cordova 3.4的Cordova / PhoneGap应用程序。 My first task was to implement a Client-Side Routing framework to make it easier to navigate between pages. 我的第一个任务是实现客户端路由框架,以使其更容易在页面之间导航。 I chose Flatiron Director as my client-side router, but when I went to implement it I started to get weird functionality out of the app. 我选择Flatiron Director作为我的客户端路由器,但是当我实现它时,我开始从应用程序中获取奇怪的功能。

My first router setup: 我的第一个路由器设置:

    var routing = {
        testHandler: function(){
            console.log('Route ran');
        },
        routes: function(){
            return {
                "/testhandler": testHandler
            }
        }
    };
    console.log('Routes added');

The routes are added (at least based on the console output). 添加路由(至少基于控制台输出)。 When I attempt to hit the /testhandler hash, I receive a "Failed to load resource: file:///testhandler" error when I set window.location.hash to "/testhandler". 当我尝试打入/ testhandler哈希值时,将window.location.hash设置为“ / testhandler”时,会收到“无法加载资源:file:/// testhandler”错误。 I noticed the "Route ran" statement was never printed. 我注意到从未打印过“ Route ran”语句。

My next attempt was just using the hashchange event with jQuery. 我的下一个尝试只是将hashchange事件与jQuery一起使用。

$(window).on('hashchange', function(){ console.log('Ran'); });

On this attempt, regardless of what I change the hash to, I see the 'Ran' output, but I still receive the "Failed to load resource: " error. 进行此尝试时,无论我将哈希值更改为什么,我都会看到“ Ran”输出,但仍然收到“无法加载资源:”错误。

Is this a problem with PhoneGap/Cordova? 这是PhoneGap / Cordova的问题吗? Or our implementation? 还是我们的实施? Is it just not possible to use client-side routing with Cordova? 难道无法在Cordova中使用客户端路由? What am I doing wrong? 我究竟做错了什么?

I know that this doesn't answer your question directly but you may consider making your own provisional router. 我知道这并不能直接回答您的问题,但是您可以考虑制作自己的临时路由器。 This may help you to debug your app and to figure out what's the problem. 这可以帮助您调试应用程序并找出问题所在。

Something like this for example: 例如:

   var router = (function (routes) {
        var onRouteChange = function () {
            // removes hash from the route
            var route = location.hash.slice(1);

            if (route in routes) {
                routes[route]();
            } else {
                console.log('Route not defined');
            }
        };

        window.addEventListener('hashchange', onRouteChange, false);

        return {
            addRoute: function (hashRoute, callback) {
                routes[hashRoute] = callback;
            },
            removeRoute: function (hashRoute) {
                delete routes[hashRoute];
            }
        };
    })({
            route1: function () { 
                console.log('Route 1');
                document.getElementById('view').innerHTML = '<div><h1>Route 1</h1><p>Para 1</p><p>Para 2</p></div>';
            }, 
            route2: function () { 
                console.log('Route 2'); 
                document.getElementById('view').innerHTML = '<div><h1>Route 1</h1><p>Para 1</p><p>Para 2</p></div>';
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM