0

grunt 任务“uglify”改变了我的代码,所以它不再工作了。我是 CoffeeScript、AngularJS、grunt 等的初学者。为了解决这个问题,我必须对我的 CoffeeScript 代码进行哪些更改?

我不知道为什么“uglify”会这样改变我的代码。JavaScript 代码对我来说看起来不错。

咖啡脚本代码:

# add background and some style just for specific page
.directive('customBackground', () ->
  return {
  restrict: "A"
  controller: [
    '$scope', '$element', '$location'
    ($scope, $element, $location) ->
      path = ->
        return $location.path()

      addBg = (path) ->
        # remove all the classes
        $element.removeClass 'body-home body-special body-tasks body-lock'

        # add certain class based on path
        switch path
          when '/' then $element.addClass 'body-home'
          when '/404', '/500', '/signin' then $element.addClass 'body-special'

      addBg $location.path()

      $scope.$watch(path, (newVal, oldVal) ->
        if newVal is oldVal
          return
        addBg $location.path()
        return
      )
      return
  ]
  }
)

编译好的JS代码

.directive('customBackground', function() {
    return {
      restrict: "A",
      controller: [
        '$scope', '$element', '$location', function($scope, $element, $location) {
          var addBg, path;
          path = function() {
            return $location.path();
          };
          addBg = function(path) {
            $element.removeClass('body-home body-special body-tasks body-lock');
            switch (path) {
              case '/':
                return $element.addClass('body-home');
              case '/404':
              case '/500':
              case '/signin':
                return $element.addClass('body-special');
            }
          };
          addBg($location.path());
          $scope.$watch(path, function(newVal, oldVal) {
            if (newVal === oldVal) {
              return;
            }
            addBg($location.path());
          });
        }
      ]
    };
  })

“丑化”任务后的 JS 代码

.directive("customBackground", function () {
    return{restrict: "A", controller: ["$scope", "$element", "$location", function ($scope, $element, $location) {
        var addBg, path;
        path = function () {
            return $location.path()
        }, addBg = function (path) {
            switch ($element.removeClass("body-home body-special body-tasks body-lock"), path) {
                case"/":
                    return $element.addClass("body-home");
                case"/404":
                case"/500":
                case"/signin":
                    return $element.addClass("body-special")
            }
        }, addBg($location.path()), $scope.$watch(path, function (newVal, oldVal) {
            newVal !== oldVal && addBg($location.path())
        })
    }]}
})
4

0 回答 0