Angular JS组织依赖关系和变量的最佳实践

时间:2017-05-18 20:05:49

标签: javascript angularjs

我正在开展一个项目,让我了解一大批海外开发人员在加入公司前一年在Angular JS中所做的事情。这是一个没有文档的庞大项目。我很好奇是否有人在代码中包含了如此多的依赖关系,然后将每个依赖项重命名为变量,如(e,t,n,r,s,a),然后将其包含在复杂的嵌套中一个控制器长140线的结构。这被认为是编码的最佳实践吗?

我感觉它比它需要的更麻烦。有没有人知道映射依赖项,变量等的有效工具或方法,并重新组织Angular JS代码?

示例代码

define(["./../_module", "moment"], function (e, t) {
"use strict";
e.controller("trendController", ["$scope", "$state", "$q", "$rootScope", "labelsService", "trendService", "gridSettingsService", "colorpickerSettingsService", "loadDisplay", "uiGridConstants", "APP_CONSTANTS", "globalErrorService", "utilsService", "usersService", "browserHelper", "pageErrorHandlingHelper", function (e, t, n, r, s, a, l, d, i, o, c, u, g, f, b, v) {
    function h(t) {
        var n = angular.copy(C);
        return n.graphColor = e.defaultColors[t], n
    }

    function D() {
        var t = n.all([O, j])["catch"](p.errorHandlingActions.loadedFailed);
        i.addDisplay(t, "", e.gridAreaId)
    }
    var p = this;
    p.errorHandlingActions = v.errorHandlingActions, e.hasMeasurementsUnitsClash = !1, e.gridAreaId = "trendWrapper", e.labels = {
        genchoosedev: "",
        genchooseobj: "",
        generror: "",
        gennodata: "",
        genhour: "",
        genday: "",
        genweek: "",
        gencustom: "",
        gendatetime: "",
        tndlink: "",
        tnddevlbl: "",
        tndptlbl: "",
        tndclrlbl: "",
        tndexport: "",
        tndtbllbl: "",
        tndgphlbl: "",
        tnddataempty: "",
        tndtime: "",
        tndpointhdr: "",
        tndgphmeas: ""
    };
    var O = s.getLabelsForPage(e.labels).then(function (t) {
        e.labels = t
    });
    e.defaultColors = c.TRENDS.DEFAULT_COLORS, e.trendPointsMax = c.TRENDS.TREND_POINTS_MAX, e.devicesObjects = [], e.defaultColorPickerOptions = d.getSettings(), e.isPredefinedFiltersUsed = !1;
    var j = a.getAllTrendDeviceObjects().then(function (t) {
            return _.forEach(t, function (e) {
                e.objects = _.sortBy(e.objects, "objName")
            }), e.devicesObjects = _.sortBy(t, "name"), f.getTrendInfoSettings(r.sessionUser.userName).then(function (t) {
                if (t.trendLines && "0" != t.trendLines[0].devInst) {
                    e.startDate = t.trendStart, e.endDate = t.trendEnd;
                    var n = [];
                    _.forEach(t.trendLines, function (t) {
                        var r = angular.copy(C);
                        r.deviceObjects = _.find(e.devicesObjects, {
                            id: t.devInst
                        }), r.selectedObject = _.find(r.deviceObjects.objects, {
                            objID: t.objID
                        }), r.graphColor = t.colorVal, n.push(r)
                    }), e.selectedDeviceObjects = n, e.isPredefinedFiltersUsed = !0, e.triggerReloadDataWithPredefinedFilters()
                }
                e.$watch("selectedDeviceObjects", function (t, n) {
                    var r = _.last(t);
                    if (angular.isDefined(r) && null !== r.selectedObject) {
                        var s = parseInt(r.selectedObject.objID);
                        !isNaN(s) && angular.isNumber(s) && t.length < e.trendPointsMax && t.push(h(t.length))
                    }
                    if (t.length >= 2) {
                        var a = t[t.length - 2];
                        null === a.selectedObject && null === r.deviceObjects && t.splice(t.length - 1, 1)
                    }
                    e.HasDataToDisplay = _.filter(t, function (e) {
                        return null !== e.selectedObject
                    }).length > 0, e.saveTrendInfoForCurrentUser()
                }, !0), e.$watchGroup(["startDate", "endDate"], function (t, n) {
                    e.saveTrendInfoForCurrentUser()
                })
            })
        }),
        C = {
            deviceObjects: null,
            selectedObject: null,
            gridOptions: null,
            graphColor: e.defaultColors[0]
        };
    e.selectedDeviceObjects = [angular.copy(C)], e.HasDataToDisplay = !1, D(), e.reloadHandler = null, e.unitsOfMeasureCounts = [], e.$watchCollection("unitsOfMeasureCounts", function (t) {
        var n = {};
        _.each(t, function (e) {
            null !== e && (n[e] ? n[e]++ : n[e] = 1)
        }), e.hasMeasurementsUnitsClash = Object.keys(n).length > 2
    }), e.TriggerDataLoad = function (t, n) {
        null === n.deviceObjects && (n.selectedObject = null), n.selectedObject ? e.unitsOfMeasureCounts[t] = n.selectedObject.units : e.unitsOfMeasureCounts[t] = null, null != e.reloadHandler && e.reloadHandler(t, n)
    }, e.colorChangedHandler = null, e.TriggerColorChanged = function (t, n) {
        null !== e.colorChangedHandler && e.colorChangedHandler(t, n)
    };
    var S = function () {
        var t = {
            preselectedDataPoints: [],
            datesRange: {
                start: e.startDate,
                end: e.endDate
            }
        };
        return _.forEach(e.selectedDeviceObjects, function (e) {
            angular.isDefined(e.deviceObjects) && null != e.deviceObjects && angular.isDefined(e.selectedObject) && null != e.selectedObject && t.preselectedDataPoints.push({
                deviceId: e.deviceObjects.id,
                objectId: e.selectedObject.objID
            })
        }), t
    };
    e.redirectToExportTrend = function () {
        var e = S(),
            n = JSON.stringify(e);
        t.go("exportTrendData", {
            paramsJson: n
        })
    }, e.saveTrendInfoForCurrentUser = function () {
        var t = {
            selectedDeviceTrends: e.selectedDeviceObjects,
            datesRange: {
                start: e.startDate,
                end: e.endDate
            }
        };
        return f.updateTrendInfoSettings(r.sessionUser.userName, t).then(function (e) {}, p.errorHandlingActions.failed)
    }, e.startDate = null, e.endDate = null, e.exportSelectedPoints = function () {
        var e = S();
        return a.exportTrendData(e.preselectedDataPoints, e.datesRange).then(function (e) {
            angular.isDefined(e) && e.length > 0 && b.saveFileAsCSV(e)
        }, p.errorHandlingActions.failed)
    }
}])

});

2 个答案:

答案 0 :(得分:1)

我非常怀疑这是原始代码,可能是一些缩小/混淆器工具的输出。 单字母变量是由缩小器应用的优化,以减小项目的大小,但它们实际上删除了空格,注释,换行等。

我的猜测是他们的目的是混淆代码,这是可以撤销的。你不能回到原来的名字。在线你可以找到一些JS beautifier,但他们只是格式化代码,使它“更好看”,如果不是,但这个代码已经格式化,所以没有帮助。

检索原始变量名称的唯一方法是将某种映射(actualName / oldName)保存在某处。

我个人会与这些人联系,了解是否有任何原始代码的痕迹。

答案 1 :(得分:0)

使用像(e,t,n,r,s,a)这样的变量不是最佳实践,因为它们没有意义,这是我如何在控制器中管理依赖关系的一个例子,我创建了函数angular.module.controller,函数的参数是我的依赖项,然后我将函数声明作为参数发送到我的function notifyCtrl($scope, notify) { $scope.msg = 'Hello! This is a sample message!'; $scope.demo = function () { notify({ message: $scope.msg, classes: $scope.classes, templateUrl: $scope.template }); }; $scope.closeAll = function () { notify.closeAll(); }; } angular .module('angularModule') .controller('notifyCtrl', notifyCtrl);

MsgBox(DriveInfo.GetDrives()(1).Name)