2012-04-11 96 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或其他任何东西是否具有此功能,但我会怀疑它。子域名应该是您网站的不同区域,因此您可能无法正确使用它们。