-1

我已将此文件包含在 Angular 应用程序中。

/**
 * @description Google Chart Api Directive Module for AngularJS
 * @version 0.1
 * @author Nicolas Bouillon <nicolas@bouil.org>
 * @license MIT
 * @year 2013
 */
(function(document, window) {
  'use strict';

  function loadScript(url, callback) {
    var script = document.createElement('script');
    script.src = url;
    script.onload = callback;
    script.onerror = function() {
      throw new Error('Error loading "' + url + '"');
    };

    document.getElementsByTagName('head')[0].appendChild(script);
  }


  angular.module('directive.googlechart', [])

  .constant('googleChartApiConfig', {
    version: '1',
    optionalSettings: {
      packages: ['corechart']
    }
  })

  .factory('googleChartApiProxy', ['$rootScope', '$q', 'googleChartApiConfig',
    function($rootScope, $q, apiConfig) {
      var apiReady = $q.defer(),
        onLoad = function() {
          // override callback function
          var settings = {
            callback: function() {
              var oldCb = apiConfig.optionalSettings.callback;
              $rootScope.$apply(function() {
                apiReady.resolve();
              });

              if (angular.isFunction(oldCb)) {
                oldCb.call(this);
              }
            }
          };

          settings = angular.extend({}, apiConfig.optionalSettings, settings);

          window.google.load('visualization', apiConfig.version, settings);
        };

      loadScript('//www.google.com/jsapi', onLoad);

      return function(fn, context) {
        var args = Array.prototype.slice.call(arguments, 2);
        return function() {
          apiReady.promise.then(function() {
            fn.apply(context, args.concat(Array.prototype.slice.call(arguments)));
          });
        };
      };
    }
  ])

  .directive('googleChart', ['$timeout', '$window', 'googleChartApiProxy',
    function($timeout, $window, apiProxy) {
      return {
        restrict: 'A',
        scope: {
          chart: '=chart'
        },
        link: function($scope, $elm, $attr) {
          // Watches, to refresh the chart when its data, title or dimensions change
          $scope.$watch('chart', function() {
            draw();
          }, true); // true is for deep object equality checking

          // Redraw the chart if the window is resized 
          angular.element($window).bind('resize', function() {
            draw();
          });

          function draw() {
            if (!draw.triggered && ($scope.chart !== undefined)) {
              draw.triggered = true;
              $timeout(function() {
                draw.triggered = false;
                var dataTable = new google.visualization.DataTable($scope.chart.data, 0.5);

                var chartWrapperArgs = {
                  chartType: $scope.chart.type,
                  dataTable: dataTable,
                  view: $scope.chart.view,
                  options: $scope.chart.options,
                  containerId: $elm[0]
                };

                if ($scope.chartWrapper == null) {
                  $scope.chartWrapper = new google.visualization.ChartWrapper(chartWrapperArgs);
                  google.visualization.events.addListener($scope.chartWrapper, 'ready', function() {
                    $scope.chart.displayed = true;
                  });
                  google.visualization.events.addListener($scope.chartWrapper, 'error', function(err) {
                    console.log("Chart not displayed due to error: " + err.message);
                  });
                } else {
                  $scope.chartWrapper.setChartType($scope.chart.type);
                  $scope.chartWrapper.setDataTable(dataTable);
                  $scope.chartWrapper.setView($scope.chart.view);
                  $scope.chartWrapper.setOptions($scope.chart.options);
                }

                $timeout(function() {
                  $scope.chartWrapper.draw();
                });
              }, 0, true);
            }
          }

          draw = apiProxy(draw, this);
        }
      };
    }
  ]);

})(document, window);

当我在做 grunt build 它在这一行给出错误

错误是

[L123:C11] W021: 'draw' is a function.
          draw = apiProxy(draw, this);

我已经尝试更改变量名,并将 var 放在它前面,但这并不能解决问题,请提出解决方案。

4

1 回答 1

1

当您尝试将值分配给最初作为函数声明的一部分出现的标识符时,JSHint 会引发“{a} 是一个函数”警告:

function draw() {
    // ...
}

draw = apiProxy(draw, this);

我不完全确定此警告背后的原因(JSLint 在这种情况下也会发出警告,但它的消息是“只读”)。

您可以通过对函数声明使用不同的标识符来修复它:

function drawFn() {
    // ...
}

var draw = apiProxy(drawFn, this);

但是,我强烈建议不要在 Grunt 文件的 JSHint 部分中包含第三方脚本。

于 2013-09-04T10:43:43.433 回答