bindTo
jQueryのバインドと同様に、bindDataをMarionetteのに渡す方法はありますか?
jQuery の Web サイトで、次のようにして bindData を渡すことができると読みました。
function myFunction(event){
console.log(event.data.foo); // output "bar"
};
$(".some-element").bind("click", {foo:"bar"}, myFunction);
これを行う理由は、複数の関数を 1 つのルートにバインドしているためです。
最初の関数はルートからのパラメーターを使用するだけで、特別なことは何もありません。
2 番目の関数にはカスタム データを渡す必要があり、そこで bindData の出番です。
コントローラーとルーター
var Controller = {
page1: function(itemId){
Vent.trigger('openPageOne', itemId);
}
};
var AppRouter = Marionette.AppRouter.extend({
controller: Controller,
appRoutes: {
"page1/:itemid" : "page1"
},
start: function() {
Backbone.history.start();
}
});
最初のバインド
これはうまく機能し、そのルートに移動すると、コンソールに itemId が出力されます。
var MyLayout = Marionette.Layout.extend({
initialize: function(){
_.bindAll(this);
},
myFunction: function(itemId){
console.log(itemId);
}
});
var myLayout = new MyLayout();
myLayout.bindTo(Vent, "openPageOne", myLayout.myFunction);
2 番目のバインド
ここで失敗します :(
カスタム データ オブジェクトを関数に
渡したいのですが、anotherFunction 内で、foo の値を表示したいのです。
var AnotherLayout = Marionette.Layout.extend({
initialize: function(){
_.bindAll(this);
},
anotherFunction: function(event){
// Here is where I want to use foo
console.log(event.data.foo);
}
});
var anotherLayout = new AnotherLayout();
anotherLayout.bindTo(Vent, "openPageOne", {foo:"bar"}, anotherLayout.anotherFunction);
より具体的に言えば、最初の関数は私のページのコンテンツを変更する必要があります。2 番目の関数は、ナビゲーション メニューの項目を強調表示する必要があります。関数に送信するカスタム オブジェクトは、基本的にメニュー項目の要素 ID であるため、それにクラスを追加できます。
私はこれに間違った方法でアプローチしていますか?どんな入力でも役に立ちます。