代码之家  ›  专栏  ›  技术社区  ›  Alex Gelinas

创建模板&在JS中使用变量替换

  •  1
  • Alex Gelinas  · 技术社区  · 9 年前

    以下是我创建的代码的浓缩版本:

            $("#engagementDaily").hide();
            $("#engagementWeekly").hide();
            $("#engagementMonthly").hide();
            $("#engagementYearly").hide();
            $("#engagementDay").click(function(){
                engGraph.options.scales.xAxes[0].time.unit = 'day'
                $(this).addClass('active');
                nullXAxes();
                $("#engagementDaily").fadeIn();
                $("#engagementWeekly").hide();
                $("#engagementMonthly").hide();
                $("#engagementYearly").hide();
                engGraph.update();
            });
            $("#engagementWeek").click(function(){
                engGraph.options.scales.xAxes[0].time.unit = 'week'
                $(this).addClass('active');
                nullXAxes();
                $("#engagementDaily").hide();
                $("#engagementWeekly").fadeIn();
                $("#engagementMonthly").hide();
                $("#engagementYearly").hide();
                engGraph.update();
            });
            $("#engagementMonth").click(function(){
                engGraph.options.scales.xAxes[0].time.unit = 'month'
                $(this).addClass('active');
                nullXAxes();
                $("#engagementDaily").hide();
                $("#engagementWeekly").hide();
                $("#engagementMonthly").fadeIn();
                $("#engagementYearly").hide();                
                engGraph.update();
            });
            $("#engagementYear").click(function(){
                engGraph.options.scales.xAxes[0].time.unit = 'year'
                $(this).addClass('active');
                nullXAxes();
                $("#engagementDaily").hide();
                $("#engagementWeekly").hide();
                $("#engagementMonthly").hide();
                $("#engagementYearly").fadeIn();
                engGraph.update();
            });
            $("#engagementAutoDay").click(function(){
                engGraph.options.scales.xAxes[0].time.min = null;
                engGraph.options.scales.xAxes[0].time.max = null;
                $(this).addClass('active');
                engGraph.update();
            });
            $("#engagementAutoWeek").click(function(){
                engGraph.options.scales.xAxes[0].time.min = null;
                engGraph.options.scales.xAxes[0].time.max = null;
                $(this).addClass('active');
                engGraph.update();
            });
            $("#engagementAutoMonth").click(function(){
                engGraph.options.scales.xAxes[0].time.min = null;
                engGraph.options.scales.xAxes[0].time.max = null;
                $(this).addClass('active');
                engGraph.update();
            });    
            $("#engagementAutoYear").click(function(){
                engGraph.options.scales.xAxes[0].time.min = null;
                engGraph.options.scales.xAxes[0].time.max = null;
                $(this).addClass('active');
                engGraph.update();
            });                    
            $("#engagement7Day").click(function(){
                engGraph.options.scales.xAxes[0].time.min = sevenDays;
                engGraph.options.scales.xAxes[0].time.max = latestDate;
                $(this).addClass('active');
                engGraph.update();
            });
            //Et Cetera, et cetera
    

    理想情况下,我希望将其转换为一个模板,该模板可以存在于脚本根范围内的对象中,在该模板中,我可以用该图表/图形的名称替换重复的元素,类似这样:

    var graphTemplate = function(currentGraph, currentGraphShort)
    

    和

            $("#" + currentGraph + "Daily").hide();
            /////
            [currentGraphShort].options.scales.xAxes[0].time.unit = 'day'
    

    但我的实验没有成功。我研究了使用模板文字;但这些似乎更适合在字符串中使用(我可以说,从Iv'e使用的内容来看);我尝试创建一个对象,例如 [currentGraphShort]["options"]["scales"]["xAxes[0]"]["time"]["unit"] = 'day'

    编辑:

    我试着添加我作为全局根范围中的函数运行的精确代码,并在图的函数中运行它;但是它无法找到我的图(engGraph)——说它是未定义的。也许这也是我遗漏的一个范围因素?

    非常感谢您的帮助!

    1 回复  |  直到 9 年前
        1
  •  0
  •   Alex Gelinas    9 年前

    最后,我创建了一个全局函数,看起来像这样:

        function graphInit(graphName, currentGraph, latestDate) {
        //Date formatting
        var sevenDays = moment(latestDate).subtract(7, 'days').format("YYYY-MM-DD HH:mm")
        var thirtyDays = moment(latestDate).subtract(30, 'days').format("YYYY-MM-DD HH:mm")
        var oneWeek = moment(latestDate).subtract(1, 'weeks').format("YYYY-MM-DD HH:mm")
        var twoWeeks = moment(latestDate).subtract(2, 'weeks').format("YYYY-MM-DD HH:mm")
        var fourWeeks = moment(latestDate).subtract(4, 'weeks').format("YYYY-MM-DD HH:mm")
        var threeMonths = moment(latestDate).subtract(3, 'months').format("YYYY-MM-DD HH:mm")
        var sixMonths = moment(latestDate).subtract(6, 'months').format("YYYY-MM-DD HH:mm")
        var oneYear = moment(latestDate).subtract(1, 'years').format("YYYY-MM-DD HH:mm")
        var twoYears = moment(latestDate).subtract(2, 'years').format("YYYY-MM-DD HH:mm")
            //nullify the xAxes scale (Used to default to auto when switching scales)
        function nullXAxes() {
            currentGraph.options.scales.xAxes[0].time.min = null;
            currentGraph.options.scales.xAxes[0].time.max = null;
            $(this).addClass('active');
            currentGraph.update();
        }
        //reset the xAxes scale to specified min/max
        function xAxesScale(min, max) {
            currentGraph.options.scales.xAxes[0].time.min = min;
            currentGraph.options.scales.xAxes[0].time.max = max;
            $(this).addClass('active');
            currentGraph.update();
        }
    
        //init scaling
        $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Monthly,#" + graphName + "Yearly").hide();
        //left-side click parameters
        $("#" + graphName + "Auto").click(function() {
            currentGraph.options.scales.xAxes[0].time.unit = null;
            $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Monthly,#" + graphName + "Yearly").hide();
            nullXAxes();
        });
        $("#" + graphName + "Day").click(function() {
            currentGraph.options.scales.xAxes[0].time.unit = 'day'
            $("#" + graphName + "Weekly,#" + graphName + "Monthly,#" + graphName + "Yearly").hide()
            $("#" + graphName + "Daily").fadeIn();
            nullXAxes();
        });
        $("#" + graphName + "Week").click(function() {
            currentGraph.options.scales.xAxes[0].time.unit = 'week'
            $("#" + graphName + "Daily,#" + graphName + "Monthly,#" + graphName + "Yearly").hide()
            $("#" + graphName + "Weekly").fadeIn();
            nullXAxes();
        });
        $("#" + graphName + "Month").click(function() {
            currentGraph.options.scales.xAxes[0].time.unit = 'month'
            $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Yearly").hide()
            $("#" + graphName + "Monthly").fadeIn();
            nullXAxes();
        });
        $("#" + graphName + "Year").click(function() {
            currentGraph.options.scales.xAxes[0].time.unit = 'year'
            $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Monthly").hide()
            $("#" + graphName + "Yearly").fadeIn();
            nullXAxes();
        });
        //right-side click parameters
        $("#" + graphName + "AutoDay,#" + graphName + "AutoWeek,#" + graphName + "AutoMonth,#" + graphName + "AutoYear").click(function() {
            nullXAxes()
        });
        $("#" + graphName + "7Day").click(function() {
            xAxesScale(sevenDays, latestDate)
        });
        $("#" + graphName + "30Day").click(function() {
            xAxesScale(thirtyDays, latestDate)
        });
        $("#" + graphName + "1Week").click(function() {
            xAxesScale(oneWeek, latestDate)
        });
        $("#" + graphName + "2Week").click(function() {
            xAxesScale(twoWeeks, latestDate)
        });
        $("#" + graphName + "4Week").click(function() {
            xAxesScale(fourWeeks, latestDate)
        });
        $("#" + graphName + "3Month").click(function() {
            xAxesScale(threeMonths, latestDate)
        });
        $("#" + graphName + "6Month").click(function() {
            xAxesScale(sixMonths, latestDate)
        });
        $("#" + graphName + "1Year").click(function() {
            xAxesScale(oneYear, latestDate)
        });
        $("#" + graphName + "2Year").click(function() {
            xAxesScale(twoYears, latestDate)
        });
    };
    

    推荐文章