2012-04-28 58 views
2

我的代碼如下:Backbone.js的路線不火正確

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" attibute。但每次點擊鏈接時,它總是在螢火蟲中顯示security error。其實我只有一個index.html頁面,我想要做的就是在其上附加一個字符串來製作一個假的html頁面,如folder/index.html/category/num1,所有的東西仍然會在當前頁面中呈現。但鏈接徘徊時顯示給我的網址是folder/category/num1。因爲這個路徑實際上不存在於我的文件夾中,所以我認爲這就是它顯示安全錯誤的原因。

那麼我該如何解決?我應該創建另一個HTML頁面和相應的文件夾嗎?或者我可以在一個index.html頁面中創建所有路由?

回答

1

嘗試把在href一個#,像

href = "#category/num1" 
+0

THX,網址是接近我預期,但相應的路線仍然不正確觸發。 – chaonextdoor 2012-04-28 03:57:23