【问题标题】:Is there any shorter way to do function calls with Ajax in it?有没有更短的方法来使用 Ajax 进行函数调用?
【发布时间】:2012-12-22 14:34:50
【问题描述】:

我将整个函数粘贴在这里,顺便说一下,我在这个脚本中使用了 mustache 模板库,但这在问题中不是必需的:

tmplReplaceContent : function(json, tmpl, target){
    var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}");
    var template = '';
    var view = '';
    /* json -> check if object */
    if (typeof json == 'object') {
        view = json;
        if(!regex.test(tmpl)){
            /* get mustache tmpl from the path */
            $.get(msi.vars.tmpl_url + tmpl + '.mustache', function(tmplOut){
                template = tmplOut;
                var content = Mustache.render(template, view);
                $(target).html(content).hide().fadeIn();
            });
        } else {
            template = tmpl;
            var content = Mustache.render(template, view);
            $(target).html(content).hide().fadeIn();
        }
    } else {
        /* getJSON from the path */
        $.getJSON(msi.vars.base_url + json, function(jsonOut){
            view = jsonOut;
            if(!regex.test(tmpl)){
                /* get mustache tmpl from the path */
                $.get(msi.vars.tmpl_url + tmpl + '.mustache', function(tmplOut){
                    template = tmplOut;
                    var content = Mustache.render(template, view);
                    $(target).html(content).hide().fadeIn();
                });
            } else {
                template = tmpl;
                var content = Mustache.render(template, view);
                $(target).html(content).hide().fadeIn();
            }
        });
    }

我无法缩短它并删除重复的代码,因为我无法在 Ajax 成功中分配局部变量,因为它是异步的。我在互联网上徘徊了大约 15 个小时。但仍然没有运气。

我怎样才能删除重复的代码并缩短这段代码?

【问题讨论】:

    标签: javascript dry shortest-path non-repetitive


    【解决方案1】:
    tmplReplaceContent : function(json, tmpl, target) {
    
      var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}"),
          view = '';
    
      function setOutput(template) {
          var content = Mustache.render(template, view);
          $(target).html(content).hide().fadeIn();
      }
    
      function doJSON(json) {
          view = json;
          if(!regex.test(tmpl)){
              /* get mustache tmpl from the path */
              $.get(msi.vars.tmpl_url + tmpl + '.mustache', setOutput);
          } else {
              setOutput(tmpl);
          }
      }
    
      /* json -> check if object */
      if (typeof json === 'object') {
        doJSON(json);
      } else {
          /* getJSON from the path */
          $.getJSON(msi.vars.base_url + json, doJSON);
    }
    

    【讨论】:

    • 你是不是故意不给 setOutput 和 doJSON 添加参数?
    • 两者都有参数,选择映射 $.getJSON & $.get 成功函数。
    【解决方案2】:

    好吧,如果你有重复的代码,函数就在那里:)

    只是为了好玩,这里是你如何包含一个:

    tmplReplaceContent : function(json, tmpl, target){
    
        function render(tmplOut) {
            template = tmplOut;
            var content = Mustache.render(template, view);
            $(target).html(content).hide().fadeIn();
        }
    
        var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}");
        var template = '';
        var view = '';
        /* json -> check if object */
        if (typeof json == 'object') {
            view = json;
            if(!regex.test(tmpl)){
                /* get mustache tmpl from the path */
                $.get(msi.vars.tmpl_url + tmpl + '.mustache', render);
            } else {
                render();
            }
    // Etc...
    

    缩短了很多东西,对吧?

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      相关资源
      最近更新 更多