骨干路由器+重写规则不触发路由与" /" [英] Backbone Router + RewriteRule not triggering route with a "/"

查看:145
本文介绍了骨干路由器+重写规则不触发路由与" /"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换 example.com/action 所有链接 example.com/index.html#action 这然后,由我的骨干路由器解析。

不过,我的新的一页,注册/:inviteAuthHash (如注册/ 9d519a2005b3dac8802d8e9e416239a1 )不工作;呈现的唯一事情是我的 index.html的,并没有我的断点在我的路由器都得到满足。

的.htaccess:

 #不是一个文件或目录
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
#example.com/home => example.com/index.html#home
重写规则^(。+)$的index.html#$ 1 [L,QSA]
 

router.js

  VAR AppRouter = Backbone.Router.extend({
    路线:{
        / *页* /
        //测试版
        request_invite':'request_invite,
        注册/:inviteAuthHash':'注册',

        // 默认
        *行动':'DEFAULTACTION
    },

    / *页* /
    // ETA
    request_invite:函数(){
        新BetaLayout;
        新RequestInviteView;
    },
    注册:功能(inviteAuthHash){
        //验证身份验证哈希
        如果(!InviteRequest.isValidInviteAuthHash(inviteAuthHash))
            返回this.defaultAction();
        其他 {
            新BetaLayout;
            新SignupView(SignupView);
        }
    },

    // 默认
    DEFAULTACTION:函数(动作){
        app_router.navigate('request_invite');
        this.request_invite();
    }
});

VAR初始化=功能(){
    app_router =新AppRouter;

    $('A')。住(点击,函数(){
        VAR HREF = $(本).attr(HREF');

        //仅定位到真正的链接
        如果(HREF = =未定义)
            返回;

        //在新窗口中打开?
        如果($(本).prop(目标)==_blank)
            返回true;

        app_router.navigate(HREF,{触发:真});

        返回false;
    });

    Backbone.history.start({pushState:真});
};
返回 {
    初始化:初始化
};
 

解决方案

您可以试试这个的.htaccess来代替:

 #不是一个文件或目录
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-l
#example.com/home => example.com/index.html#home
重写规则(?!^指数\ html的)^(。+)$ /index.html#$1 [L,NC,R,NE]
 

I convert all links from example.com/action to example.com/index.html#action which is then parsed by my Backbone router.

However, my new page, signup/:inviteAuthHash (eg. signup/9d519a2005b3dac8802d8e9e416239a1) is not working; the only thing that renders is my index.html, and none of my breakpoints in my router are met.

.htaccess:

# not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# example.com/home => example.com/index.html#home
RewriteRule ^(.+)$ index.html#$1 [L,QSA]

router.js

var AppRouter = Backbone.Router.extend( {
    routes : {
        /* PAGES */
        // beta
        'request_invite' : 'request_invite',
        'signup/:inviteAuthHash' : 'signup',

        // Default
        '*actions' : 'defaultAction'
    },

    /* PAGES */
    // eta
    request_invite : function() {
        new BetaLayout;
        new RequestInviteView;
    },
    signup : function(inviteAuthHash) {
        // validate auth hash
        if(!InviteRequest.isValidInviteAuthHash(inviteAuthHash))
            return this.defaultAction();
        else {
            new BetaLayout;
            new SignupView(SignupView);
        }
    },

    // Default
    defaultAction : function(actions) {
        app_router.navigate('request_invite');
        this.request_invite();
    }
});

var initialize = function() {
    app_router = new AppRouter;

    $('a').live('click', function() {
        var href = $(this).attr('href');

        // only navigate to real links
        if(href == undefined)
            return;

        // open in new window?
        if($(this).prop('target') == '_blank')
            return true;

        app_router.navigate(href, {trigger: true});

        return false;
    });

    Backbone.history.start({pushState: true});
};
return {
    initialize : initialize
};

解决方案

Can you try this .htaccess instead:

# not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# example.com/home => example.com/index.html#home
RewriteRule (?!^index\.html)^(.+)$ /index.html#$1 [L,NC,R,NE]

这篇关于骨干路由器+重写规则不触发路由与" /"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆