【问题标题】:Backbone.js routes don't fire correctlyBackbone.js 路由无法正确触发
【发布时间】:2012-04-28 03:32:29
【问题描述】:

我的代码如下:

var AppRouter = Backbone.Router.extend({

    _data: null,
    _length: 0,
    _index: null,
    _todos: null,

    routes: {
        "*action": "index",
        "category/:name": "hashcategory"  
    },

    initialize: function(options){
        this._data = options.data;
        this._todos = new TodosCollection(options.data);
        this._length = this._todos.length;
        this._index = new CategoriesView({collection: this._todos});
    },

    index: function(){
        this._index.render();
    },

    hashcategory: function(name){
        console.log('started');
    }
});

initializeRouter = function (router) {
    Backbone.history.start({ pushState: true });
    $(document).on('click', 'a:not([data-bypass])', function (evt) {

        var href = $(this).attr('href');
        var protocol = this.protocol + '//';

        if (href.slice(protocol.length) !== protocol) {
            evt.preventDefault();
            router.navigate(href, true);
        }
    });
    return router;
};


var start = function(){ 
    p = $.ajax({
        url: 'data/todolist.json',
        dataType: 'json',
        data: {},
        success: function(data) {
            var approuter = initializeRouter(new AppRouter({data: data}));
        }
    });     
};

我的 html 中有一个 <a> 链接,该链接具有 href = "category/num1" 属性。但是每次我点击链接时,它总是在萤火虫中显示一个security error。实际上我只有一个 index.html 页面,我想做的是在其上附加一个字符串以制作一个像 folder/index.html/category/num1 这样的假 html 页面,并且所有内容仍将呈现在当前页面中。但是当链接悬停时显示给我的网址是folder/category/num1。因为我的文件夹中实际上不存在此路径,所以我认为这就是它显示安全错误的原因。

那我该如何解决呢?我应该创建另一个 html 页面和相应的文件夹吗?或者我可以在一个 index.html 页面中完成所有路由吗?

【问题讨论】:

    标签: backbone.js backbone-routing


    【解决方案1】:

    尝试在 href 中添加#,例如

    href = "#category/num1"
    

    【讨论】:

    • thx,网址接近我的预期,但相应的路由仍然无法正确触发。
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2013-09-19
    • 1970-01-01
    • 2016-12-25
    相关资源
    最近更新 更多