2011-11-14 10 views
6

मेरे पास डोम पर कुछ नेविगेशन लिंक हैं जब ऐप शुरू होता है जिसे मैं बैकबोन के माध्यम से प्रगतिशील वृद्धि के लिए हाइजैक करना चाहता हूं (रीढ़ की हड्डी ऐप के अंतर्गत एक बहुत ही स्थिर टेक्स्ट सामग्री संस्करण है)।बैकबोन राउटर घटनाओं को आग नहीं करेगा

एचटीएमएल इस तरह दिखता है:

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

तब मेरे App का उपयोग instantiated है:

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(); 
}); 

कार्यक्षेत्र सिर्फ एक रूटर और app से उन मार्गों को सही ढंग से राउटर में के माध्यम से कार्रवाई की जाती है है प्रारंभिक कार्य, लिंक भी काम करते हैं और URL को अपेक्षित रूप से बदलते हैं और पुराने ब्राउज़र पर हैंश के साथ करते हैं। समस्या यह है कि राउटर/वर्कस्पेस के भीतर कभी भी कोई कॉलबैक नहीं किया जाता है। यह बस मूक है और does not कार्यों आग, यदि क्लिक

यहाँ मेरी कार्यस्थान/रूटर है बना रहे हैं:

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 
} 
}); 

उत्तर

2

जब initialize समारोह चलाया जाता है मार्गों पहले से ही बंधी हुई (the backbone source देखें)।

this.routes = tmpr; 
this._bindRoutes(); 
:

आप मार्गों _bindRoutes समारोह के साथ बंधन ट्रिगर कर सकते हैं

संबंधित मुद्दे