これは確かに可能ですが、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や他の何かがこの機能を持っているかどうかは分かりませんが、私はそれを疑うでしょう。サブドメインはサイトの別個の領域であるため、正しく使用していない可能性があります。