Javascript 主干渲染,但插件不';使用路由器时不工作
我使用的是一个用于下拉列表的插件: 我已经让它在主干中工作,但我必须手动激活它,并确保它在渲染完成后运行。当我刷新页面时,它会工作,但是如果我离开页面,然后返回页面,插件就不会生效。渲染功能每次都在运行,所以我不知道为什么它在我正常导航时不工作Javascript 主干渲染,但插件不';使用路由器时不工作,javascript,jquery,backbone.js,jquery-plugins,Javascript,Jquery,Backbone.js,Jquery Plugins,我使用的是一个用于下拉列表的插件: 我已经让它在主干中工作,但我必须手动激活它,并确保它在渲染完成后运行。当我刷新页面时,它会工作,但是如果我离开页面,然后返回页面,插件就不会生效。渲染功能每次都在运行,所以我不知道为什么它在我正常导航时不工作 render: function() { setTimeout(function(){ $(function(){ var $selects = $('select');
render: function() {
setTimeout(function(){
$(function(){
var $selects = $('select');
$selects.easyDropDown({
cutOff: 5,
wrapperClass: 'dropdown',
onChange: function(selected){
// do something
}
});
});
}, 0);
console.log("Rendering");
this.$el.html(template());
return this;
}
这是我的路由器代码:
return Backbone.Router.extend({
initialize: function() {
// Render the layout view only once and simple change the contents of #content
// as per the desired route
var $body = $('body');
var layoutView = new LayoutView({ el: $body }).render();
this.$el = $("#content", $body);
this.currentView = null;
// Init the subrouters
this.bookRouter = this.addSubRouter(BookRouter, "books");
this.quoteRouter = this.addSubRouter(QuoteRouter, "quotes");
this.employeeRouter = this.addSubRouter(EmployeeRouter, "employees");
this.saleRouter = this.addSubRouter(SaleRouter, "sales");
// When the route changes we want to update the nav
this.bind("route", _.bind(this.updateNav, this));
},
// These are the base routes
// Other routes can be attached by creating subroutes
routes: {
// viewIndex is the main site index
// All other routes are handled by sub-routers
"": "viewIndex",
"upload": "upload",
"export": "export",
"test": "test",
},
// Add a sub route at the given route and listen for events
addSubRouter: function(subRouterClass, route) {
var router = new (subRouterClass)(route, { createTrailingSlashRoutes: true });
router.on("view", _.bind(this.switchView, this));
router.on("route", _.bind(function(route, section) {
this.trigger("route", route, section);
}, this));
return router;
},
// Change from this.currentView to newView
switchView: function(newView) {
// Do we need to remove the old view?
if (this.currentView) {
this.currentView.remove();
}
this.currentView = newView;
// Add the new view
this.$el.append(newView.render().$el);
newView.addedToDOM();
},
updateNav: function(route, section) {
// Get hold of the nav element
var $nav = $("#nav");
// Clean up the route string
route = route.replace("route:", "");
// Remove the currently active item
$(".active", $nav).removeClass("active");
// Apply .active to any navigation item that has a matching data-route attribute
$("[data-route=\"" + route + "\"]", $nav).addClass("active");
},
viewIndex: function () {
var view = new IndexView();
this.switchView(view);
},
upload: function (){
var view = new UploadIndexView();
this.switchView(view);
},
export: function() {
var view = new ExportIndexView();
this.switchView(view);
},
test: function() {
var view = new TestIndexView();
this.switchView(view);
}
});
})) 这听起来和垃圾收集有关。当您导航到另一个视图时,如何处理视图?如果你不删除它们,我想它们会继续存在,因为
onChange
正在链接到它。也许旧的观点没有被正确地删除,并且正在某处窃取行动。或者,在渲染该视图后,您可能正在破坏该视图,从而影响该视图的行为?你的路由器代码是什么?@RustyToms老实说,我不知道它是如何工作的。看起来好像视图已被删除和替换。请参阅上面的代码