[英]Declaring Marionette/Backbone Router without unused variable - JSLint
I'm in a refactoring stage right now, and I'm getting the familiar 'Unused variable' JSLint when declaring and instantiating a Marionette Router. 我现在处于重构阶段,在声明和实例化木偶路由器时,我得到了熟悉的“未使用变量”JSLint。 Here is my code: 这是我的代码:
App.Application.addInitializer(function () {
var globalRouter = new App.Routers.GlobalRouter({
controller: new App.Controllers.GlobalController()
});
//Start Backbone History
Backbone.history.start();
});
The error I get is: 我得到的错误是:
unused 'globalRouter'
Basically, I'm using a variable declaration to create the router before I start my app. 基本上,我在启动应用程序之前使用变量声明来创建路由器。 I'm trying to avoid doing this: 我试图避免这样做:
new App.Routers.GlobalRouter({
controller: new App.Controllers.GlobalController()
});
As it just introduces a different JSLint error, saying 'Do not use 'new' for side effects'. 因为它只是引入了一个不同的JSLint错误,说'不要使用'new'用于副作用'。 Does anyone have any advice on this? 有没有人对此有任何建议?
In addition, I've tried adding: 另外,我试过添加:
/*jslint unparam: true */
but this does not work :( 但这不起作用:(
The following code is debatable, but you could use an IIFE like this: 以下代码值得商榷,但您可以像这样使用IIFE:
App.Application.addInitializer(function () {
(function(){
return new App.Routers.GlobalRouter({
controller: new App.Controllers.GlobalController()
});
}());
});
Or use an intermediate variable: 或者使用中间变量:
App.Application.addInitializer(function () {
(function(){
var router = new App.Routers.GlobalRouter({
controller: new App.Controllers.GlobalController()
});
return router;
}());
});
On a side note, since you're refactoring, I would start history
in an "initialize:after" listener to ensure all routing controller have been initialized before a routing event is triggered. 在旁注中,由于您正在重构,我将在“initialize:after”侦听器中启动history
,以确保在触发路由事件之前已初始化所有路由控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.