2012-04-11 11 views
2

url(variable1.domain.com/variable2)から2つの変数を取得するシステムを構築しています。動的/ワイルドカードサブドメインルーティングでbackbone.jsを使用することはできますか?

バックボーンのサブドメインで何かを行う方法を示すドキュメントは見つかりません。 Default_urlはdomain.com/apiとして渡されます。私は、クロスドメインコールを可能にするCORS(www.enable-cors.org)というものを見つけましたが、ダイナミックドメインについては何も言及していません。

これはバックボーンでも可能ですか?そうでない場合は、ember.jsやその他のバックボーンのようなシステムにこの "機能"があるかどうかを知っていますか?

答えて

2

これは確かに可能ですが、Backboneのデフォルト動作の範囲内にはありません。あなたはこのようになります解決策をハック可能性があり、すべてのサブドメインが同じルータのコードを利用すると仮定すると:

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

一つのこのアプローチの重要な注意点は:それは自分自身をURLへナビゲートするユーザーのため正常に動作しますが、すぐにアプリケーションがRouterへのnavigateコールを実行する必要がある場合には、不安定になります。バックボーンはURLのハッシュ部分のみにナビゲートするので、サブドメインは含まれません。他の操作を行う前に、window.locationを設定するカスタムナビゲーション機能を起動する必要があります。

明らかにこれはおそらく何かではありません。バックボーンは、これに適しています。 Emberや他の何かがこの機能を持っているかどうかは分かりませんが、私はそれを疑うでしょう。サブドメインはサイトの別個の領域であるため、正しく使用していない可能性があります。