【问题标题】:jsHint throwing 'Firebase' is not defined warning [duplicate]jsHint抛出'Firebase'未定义警告[重复]
【发布时间】:2014-09-12 19:49:33
【问题描述】:

如何正确定义 Firebase 以使 jshint 停止发出哔哔声。

我的代码正在运行……只是 jshint 很烦人

app.js

 angular
  .module('morningharwoodApp', [
    'firebase',
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch'

  ])

ma​​in.js

angular.module('morningharwoodApp')
  .controller('MainCtrl', function ($scope, $firebase) {
    // var Firebase;
    var pageRef = new Firebase('https://morningharwood.firebaseIO.com/page');
    // var pageRef = new Firebase('https://morningharwood.firebaseIO.com/page');


    //init
    $scope.pages = $firebase(pageRef);
    $scope.newPage = {
        title: '',
        slug: '',
        url: '',
        desc: '',
        active: false,
        template: [
            {
                type: ''
            }
        ],
        img: '',
        dateCreated: '',
        dateUpdated: ''

    };

    //CRUD

    //add
    $scope.addPage = function() {
        $scope.pages.$add($scope.newPage);
        $scope.newPage = '';
    };
  });

【问题讨论】:

  • 您可以在文件顶部添加/*global Firebase */
  • 有趣...在顶部添加那个简单的注释修复了它。 Jshint 必须扫描 cmets?
  • 您也可以将其添加为 Angular 常量,然后在需要的地方注入 Firebase/* global window:false */ angular.module('myApp').constant('Firebase', window.Firebase);

标签: angularjs firebase jshint angularfire


【解决方案1】:

由于Firebase应该被添加到全局对象(窗口),你可以使用$window服务:

.controller('MainCtrl', function ($firebase, $scope, $window) {
    var pageRef = new $window.Firebase('...');

【讨论】:

  • 我应该把firebase放在rootscope上吗?
  • 我没有看到任何好处,但我想你可以。虽然 jsHint 可能会再次抱怨,除非你使用 $window.Firebase
  • 我不会这样做。使用上面提到的注释符号或下面提到的全局选项会更干净。
  • Firebase 不应以这种方式包含在 Angular 中。这打破了 e2e 测试和模拟。相反,应该使用正常的依赖注入来包含它。
  • @Kato:到底是怎么回事?这就是他们在 official demo (new Firebase(...)) 中使用它的方式。问题是如何让 jsHint 对此感到高兴。 @David:我看不出它有多干净,但我很想知道(也许我缺少一些东西):)
【解决方案2】:

您还可以在 jshint.rc 中执行以下操作

 "jshint_options":
    {
        "predef": {
            "Firebase": false
        }
     }

【讨论】:

  • 这是正确答案。或者在更新的版本中,您可以使用 predef 代替 globals
  • @Kato 您的评论似乎有点不清楚。看起来predef 已被删除。 github.com/jshint/jshint/issues/1914
  • @OzzieOrca 现在应该是什么?
  • 我也有点困惑,但似乎 predef 已被弃用或将被弃用。它仍在文档中。似乎目标是用globals 替换predef,但globals 没有predef 的所有相同功能。查看stackoverflow.com/questions/22551402/… 并查看github.com/jshint/jshint/issues/1914 上所有引用的问题
  • 适用于全局变量:"globals": {"firebase": false}
猜你喜欢
  • 2013-04-09
  • 2012-02-13
  • 2015-11-14
  • 2013-04-22
  • 2014-03-16
  • 2021-07-01
  • 1970-01-01
  • 2012-09-18
相关资源
最近更新 更多