バックボーンで PubSub タイプの機能を実装するより良い方法を探しています。私は現在これを達成していますが、それを行うためのより良い方法を探しています。
サンプル ルーティング コード
var AppRouter = Backbone.Router.extend({
customEvents: _.extend({}, Backbone.Events),
routes: {
"login": "login"
},
//Injecting custom event
login: function(){
this.before(function () {
app.showView('#Content', new LoginView({customEvents:this.customEvents}), "login", true);
});
},
//Injecting custom event
otherView: function(){
this.before(function () {
app.showView('#Header', new OtherView({customEvents:this.customEvents}), "login", true);
});
},
..... more code
});
サンプル ビュー コード。bind を使用して customEvent をリッスンしていることに注意してください。これはうまくいきますが、別の方法を探しています
LoginView = Backbone.View.extend({
initialize: function(options){
this.customEvents = options.customEvents;
this.customEvents.bind('app:loggedin', this.loggedIn);
},
loggedIn: function(){
console.log("LOG CHANGED");
},
...more code
ビューで使用している残りのイベントと一緒にイベントを保持したいと思います。これを達成する方法がわからない。View クラスを拡張する必要がありますか?
ビューでやりたいこと
LoginView = Backbone.View.extend({
events: {
"app:loggin" : "loggedIn"
},
loggedIn: function(){
console.log("LOG CHANGED");
},
...more code