Backbone.js路由无法正确触发

时间:2012-04-28 03:32:29

标签: backbone.js backbone-routing

我的代码如下:

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"属性。但是每次我点击链接时,它总会在firebug中显示security error。实际上我只有一个index.html页面,我想要做的是为它添加一个字符串来制作一个假的html页面,如folder/index.html/category/num1,所有的东西仍将在当前页面中呈现。但链接悬停时向我显示的网址是folder/category/num1。因为我的文件夹中实际上不存在此路径,所以我认为这就是它显示安全性错误的原因。

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

1 个答案:

答案 0 :(得分:1)

尝试在href中添加#,例如

href = "#category/num1"