我有两个视图文件views.js 和views2.js。在按钮上单击views.js 调用。在views.js 上有一个按钮需要触发views2.js。但是问题是按钮上单击views2.js 不是在职的。
我写了一些代码。在 view.js 中
define( ["backbone"], function(Backbone) {
var UserView = Backbone.View.extend({
tagName: "div",
render: function(){
this.$el.html(this.model.get("desc") + "<button id='btn2' class='btn btn- primary'>Next Page</button>");
return this;
}
});
var NextPageView = Backbone.View.extend({
events: {
'click': 'firstquestion'
},
render: function(){
return this;
},
firstquestion: function(){
//to solve circular dependency
require(['routes'],function(routes){
var router = routes.pageRouter;
router.navigate('view', {trigger: true});
});
}
});
var views2 = require("views2");//should I call views2 here
var np2 = new views2.NextPageView2({el: $("#btn2")});
return {
UserView: UserView,
NextPageView: NextPageView
};
});
在views2.js中
define( ["backbone"], function(Backbone) {
var UserView2 = Backbone.View.extend({
tagName: "div",
render: function(){
this.$el.html(this.model.get("desc") + "<button id='btn3' class='btn btn-primary'>Next Page</button>");
return this;
}
});
var NextPageView2 = Backbone.View.extend({
events: {
'click': 'secondquestion'
},
render: function(){
return this;
},
secondquestion: function(){
require(['routes2'],function(routes2){
var router = routes2.pageRouter2;
router.navigate('view', {trigger: true});
});
}
});
return {
UserView2: UserView2,
NextPageView2: NextPageView2
};
});