2012-04-11 94 views
2

我正在構建一個將從url(variable1.domain.com/variable2)中提取2個變量的系統。可能使用backbone.js動態/通配符子域路由?

我找不到任何文檔顯示如何做任何事情與骨幹子域。 Default_url只是作爲domain.com/api傳遞。我確實找到了一種叫做CORS(www.enable-cors.org)的軟件,它可以實現跨域調用,但它沒有提到動態域名。

這樣的事情甚至可能與骨幹?如果沒有,有人知道如果ember.js或其他骨幹式系統有這個「功能」?

回答

2

這當然是可能的,但不在骨幹的默認行爲範圍內。假設所有的子域採用相同的路由器代碼,你可以砍了一個解決方案可能是這樣認爲:

var Router = Backbone.Router.extend({ 
    routes: { 
    '*variables': 'buildRoute' 
    }, 

    subdomain: function() { 
    // This is probably not the prettiest/best way to get the subdomain 
    return window.location.hostname.split('.')[0]; 
    }, 

    buildRoute: function(variables) { 
    // `variables` are all your hash variables 
    // e.g., in the URL http://variable1.domain.com/#variable3=apples&variable4=oranges 
    // `variables` here would be the string 'variable3=apples&variable4=oranges' 
    // so you would have to parse that string into a JSON representation, but that's trivial 
    // Once you have the JSON, you can do something like: 
    myView.render(this.subdomain(), variablesJSON); 
    // Your view's `render` function then has the subdomain and all the variables from the URL, 
    // so it can use them appropriately. 
    } 
}); 

這種方法的一個重要的警告:它工作正常,爲用戶導航到網址本身,而是會迅速當您的應用程序需要執行navigate調用Router時變得不可靠。 Backbone只會導航到URL的哈希部分,所以它不會包含子域名。在做任何其他事情之前,您可能必須啓用自定義導航功能,設置window.location

很明顯,這可能不是Backbone非常適合的東西。我不確定Ember或其他任何東西是否具有此功能,但我會懷疑它。子域名應該是您網站的不同區域,因此您可能無法正確使用它們。