2

I am getting Error: Grid argument is not a Grid object only in prod (its working in dev), after javascript minification.

I delved into the ui.grid's code, and my initial thought is that there is a bug but since no one seemed to complain I thought I am doing something wrong.

The reason I think its a bug caused by minification:

The error occurs here, where n in dev is a function of type 'Grid', but after being minified its of type 'h'

if ("Grid" !== t.type(n))
    throw new Error("Grid argument is not a Grid object");

t.type gets the type of the function by using Function.prototype.toString. The type's name has been changed during minification, and will not equal "Grid" as the condition above demands

h.type = function(e) {
    var t = Function.prototype.toString.call(e.constructor);
    return t.match(/function (.*?)\(/)[1]

I am using Rails 4, latest version of angular and ui.grid.

Though I think it's irrelevant, here is my code:

View:

<div ng-controller="SummonersCtrl">

  <p>
    <strong>Name:</strong>
    <span>{{summoner.name}}</span>
  </p>

  <p>
    <strong>Region:</strong>
    <span>{{summoner.region}}</span>
  </p>

   <div id="rune-statistics">
    <h2>Runepage Usage</h2>
    <p>Stats of {{summoner.sumOfGames()}} games.</p>
    <div ui-grid="gridOptions" class="stats-grid"></div>
  </div>

</div>

Ctrl:

runestats = angular.module('runestats')

    summonersCtrl = runestats.controller 'SummonersCtrl'
    , ['Summoner', 'backendApi', '$rootScope', '$scope', '$http', '$log'
    , (Summoner, backendApi, $rootScope, $scope, $http, $log) ->
      $scope.summoners = []
      $scope.gridOptions =
        data: 'summoner.total_runestats'

      backendApi.getSummoners().success (data) ->
        $scope.summoners = data

      $rootScope.$on 'summonerSearched', (event, name, region) ->
        backendApi.getSummoner(name, region).success (data) ->
          $scope.summoner = new Summoner(data)
          $rootScope.$broadcast('summonerFound', name, region)
    ]

Is this a bug, or am I doing something wrong?

Vall3y
  • 1,181
  • 8
  • 18
  • It believe too it is indeed a bug in the latest version `3.x`. But it is not in a production ready state yet, that probably why noone complain about it. – runTarm Aug 16 '14 at 18:02
  • _"The type's name has been changed during minification, and will not equal "Grid" as the condition above demands"_ By curiosity, what is the type _before_ and _after_ minification ? – Sylvain Leroux Aug 16 '14 at 18:03
  • t.type(n) equals "Grid" before minification, and equals "" after minification. – Vall3y Aug 17 '14 at 04:40

1 Answers1

0

This answer may depend on what you are using for minification but a lot of tools have a setting called "mangle" which would explain a type's name being changed during minification.

where n in dev is a function of type 'Grid', but after being minified its of type 'h'

See the answer here on how some tools (that particular linked example uses Grunt) that minify also by default mangle code to save additional bytes. So turn off mangle and n should remain a function of type Grid instead of being mangled to be of type h. How to turn off mangle again would depend on the tool you are using to minify.

Of course as a work-around/last resort if you are not able to turn off the mangle feature another option is to skip minification for that file. If you manually rename a file to end with -min.js or .min.js before you minify your project then that file should be skipped as it will be assumed to already be minified.

Community
  • 1
  • 1
laurenOlga
  • 721
  • 2
  • 10
  • 21