2011-11-14 41 views
6

Tôi có một số liên kết điều hướng trên dom khi ứng dụng được khởi động mà tôi muốn chiếm đoạt để tăng cường tiến bộ thông qua Backbone (có phiên bản nội dung văn bản rất tĩnh nằm bên dưới ứng dụng xương sống).Router đường trục sẽ không kích hoạt sự kiện

Các html trông như thế này:

... 
<body> 
<header> 
<nav> 
<ol> 
<li> 
<a href="/en/home">home</a> 
</li> 
<li> 
... few more links 
</header> 

Sau đó, ứng dụng của tôi được khởi tạo sử dụng:

var App = (function(fw){ 
var $  = fw; 
var workspace   = {}; 
var self  = {}; 
var lang  = "en"; 
var models  = {...}; 
var views  = {...}; 
var collections = {...}; 
self.init = function() { 
    workspace = new Workspace(
    { 
    routes: { 
    "/": "home", 
    "/home": "home", 
    "/terms": "terms", 
    "/news": "blog" 
    }, 
    lang : lang 
    }) 
} 
return self; 
}); 
var app; 
// launch 
$(document).ready(function() { 
app = new App(jQuery); 
app.init(); 
}); 

Các không gian làm việc chỉ là một router và những tuyến đường từ ứng dụng được xử lý một cách chính xác vào router thông qua chức năng khởi tạo, cũng các liên kết hoạt động và thay đổi URL như mong đợi và thực hiện với các băm trên các trình duyệt cũ hơn. Vấn đề là không có callback nào được tạo trong chính Router/Workspace. Nó chỉ đơn giản câm và doesnt bắn các chức năng khi nhấp chuột được thực hiện

Dưới đây là Workspace của tôi/Router:

var Workspace = Backbone.Router.extend({ 

routes : {}, 
//functions      <------------THESE 
home: function(e){ 
    e.preventDefault(); 
    console.log("home here"); 
    App.views.HomeView.render(); 
}, 
terms: function(){   //<-----------NEVER 
    console.log("terms here"); 
    App.views.TermsView.render(); 
}, 
blog: function(){   //<-----------FIRE 
    console.log("blog here"); 
}, 
initialize: function(params){ 
    var t = this; 
    var tmpr = {}; 
    for(var i in params.routes) 
    { 
    //this just fuses lang and each route so /home becomes /en/home 
        tmpr["/"+params.lang+i] = params.routes[i]; 
    } 
    this.routes = tmpr; // this is fine and when logged it shows nicely with all routes looking good 
    // router will only ever initialize once so lets deal with the stuff currently on the DOM before the app is inited. 
    $("a",$("header")).click(function(e){ 
    e.preventDefault(); 
    Backbone.history.navigate($(this).attr("href"),true);//<-- true is set? should fire methods? 
    // I've also tried all kinds of variations like t.navigate(..) where t is this workspace 
    }); 
//this also seems to be fine and either does hashes or states 
    if(history && history.pushState) { 
    Backbone.history.start({ 
    pushState : true 
    }); 
    console.log("has pushstate"); 
    } else { 
    Backbone.history.start(); 
    console.log("no pushstate"); 
    } 
    console.log("Router inited with routes:",this.routes);//logs routes nicely 
} 
}); 

Trả lời

2

Các tuyến đường đã được ràng buộc khi initialize chức năng được chạy (xem the backbone source).

Bạn có thể kích hoạt các đường liên kết với _bindRoutes chức năng:

this.routes = tmpr; 
this._bindRoutes(); 
Các vấn đề liên quan