我是新來的主幹。我只想知道全局事件觸發並綁定每個事件的回調。我試過像這樣...主幹事件觸發和事件綁定
landing.view.js
'use strict';
var $ = require('jquery');
var Backbone = require('backbone');
var _ = require('underscore');
var tpl = require('text!components/landing-page/templates/landing-page.html');
return Backbone.View.extend({
el: '.view-container',
template: _.template($(tpl).html()),
events:{
'click .btn-show': 'showUsersListHandler'
},
initialize: function(){
this.render();
},
render: function(){
$(this.el).html(this.template);
return this;
},
showUsersListHandler: function(evt){
Backbone.trigger('userPage', {trigger:true});
}
});
another.view.js
'use strict';
var Backbone = require('backbone');
var _ = require('underscore');
var $ = require('jquery');
var tpl = require('text!components/user-logs-listing/templates/user-logs-listing-tpl.html');
return Backbone.View.extend({
el: '.users-list-container',
template: _.template($(tpl).html()),
initialize: function(){
this.render();
},
render: function(){
$(this.el).html(this.template);
}
});
應用路由器
'嚴格使用';
var $ = require('jquery');
var Backbone = require('backbone');
var viewContainer = require('inits/utils');
var landingView = require('components/landing-page/views/landing-page');
var userListingView = require('components/user-logs-listing/views/user-logs-listing.view');
return Backbone.Router.extend({
routes: {
"": "landing",
"usersList": "usersPage"
//'*path': 'defaultRoute'
},
initailize: function(){
Backbone.listenTo(Backbone, 'userPage', this.usersPage)
},
landing: function(){
viewContainer.createContainer('view-container');
new landingView();
},
usersPage: function(){
//$('.view-container').remove();
viewContainer.createContainer('users-list-container');
new userListingView();
}
});
如果我點擊登陸視圖中的按鈕,它應該觸發'userPage',它應該導航到#UsersPage。問題是如何觸發並綁定全局事件的回調?幫助我得到正確的解決方案!謝謝。
全球事件是什麼意思? – Nitesh
正如我在問題中提到的,如果我沒有錯,當我在着陸視圖中觸發'userPage'事件時,應該渲染另一個視圖。我在路由器中附加了事件響應,但它不起作用? – Navin