6

how do I define Firebase properly so jshint stops beeping.

My code is working... just jshint is annoy

app.js

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

  ])

main.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 = '';
    };
  });

enter image description here

Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
  • 3
    You could add `/*global Firebase */` at the top of the file. – runTarm Jul 22 '14 at 07:23
  • Interesting...adding that simple comment at the top fixed it. Jshint must scan for comments? – jcrowson Sep 22 '14 at 14:09
  • You could also add it as an Angular constant and then inject `Firebase`wherever you need it: `/* global window:false */ angular.module('myApp').constant('Firebase', window.Firebase);` – Scotty Waggoner Nov 17 '15 at 03:54

2 Answers2

10

You can also do the following in your jshint.rc

 "jshint_options":
    {
        "predef": {
            "Firebase": false
        }
     }
Nikos
  • 7,295
  • 7
  • 52
  • 88
  • 2
    This is the correct answer. Or in more recent versions, you would use `predef` in place of `globals` – Kato Jul 22 '14 at 13:42
  • @Kato your comment seems a little unclear. It looks like `predef` has been removed. https://github.com/jshint/jshint/issues/1914 – Scotty Waggoner Sep 11 '15 at 00:05
  • @OzzieOrca what should it be now? – Nikos Sep 11 '15 at 13:57
  • I'm slightly confused too but it seems like predef has been deprecated or is going to be. It is still in the docs. It seems like the goal is to replace `predef` with `globals` but `globals` doesn't have all of the same features `predef` did. Look at http://stackoverflow.com/questions/22551402/difference-between-globals-and-predef-in-jshintrc and look at all the referenced issues on https://github.com/jshint/jshint/issues/1914 – Scotty Waggoner Nov 17 '15 at 03:45
  • Works fine with globals: "globals": {"firebase": false} – felansu Mar 14 '17 at 11:36
3

Since Firebase is supposed to be added to the global object (window), you can use the $window service:

.controller('MainCtrl', function ($firebase, $scope, $window) {
    var pageRef = new $window.Firebase('...');
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • should I put the firebase on rootscope instead? – Armeen Moon Jul 22 '14 at 06:51
  • I don't see any benefit, but I guess you could. Although jsHint will probably complain again unless you use `$window.Firebase`. – gkalpak Jul 22 '14 at 07:02
  • I would not do it this way. It would much cleaner to use the comment notation mentioned above or the global option mentioned below. – David East Jul 22 '14 at 12:41
  • Firebase shouldn't be included this way in Angular. This breaks e2e tests and mocks. Instead, it should be included using normal dependency injection. – Kato Jul 22 '14 at 13:40
  • 2
    @Kato: How exactly ? This is how they use it in the **[official demo](https://www.firebase.com/quickstart/angularjs.html)** (`new Firebase(...)`). The question is how to make jsHint happy about it. @David: I don't see how it is cleaner, but I would be very interested to know (maybe there is something I am missing) :) – gkalpak Jul 22 '14 at 17:47
  • I believe that it's much cleaner to globally exclude this error rather than have to compensate for it on every instance of Firebase. – David East Jul 22 '14 at 22:05