重构两个 jQuery UI 自动完成功能,使其更具功能性和功能性。干燥

发布于 2024-12-04 01:07:05 字数 2220 浏览 1 评论 0原文

我在同一页面上有两个 jQuery UI 自动完成功能,我想让代码更加“实用”和简洁。我的背景几乎是严格的面向对象,我想更加认真地编写更多函数式 JavaScript。

这两个函数都是对象文字的属性,我用它来命名页面上的函数。现在,函数之间有很多重复的代码,我想使用类似于“偏应用”模式来减少代码。

自动完成 1:

initProject : function(){
    var selected = 0;
    var suggestions = []; 
    var projs;
    var len;

    $("#projectNum").autocomplete({
        source : function(req, add){
            $.getJSON("projectList.do?method=viewProjectListJSON&contractID=" + req.term, function(data){
                //clear the suggestions array
                suggestions = [];

                projs = data[0];

                len = projs.length;

                for(var i = 0; i < len; i++){
                    suggestions.push(projs[i][1]);
                };
                add(suggestions);   

            });//end getjson callback
        }, 

        minLength : 2,

        select : function(){
            thisval = $(this).val();

            for(var i = 0;i < len; i++){

                if(thisval === projs[i][1]){
                    $("#projectID").val(projs[i][0]);
                    return;
                }   
            }
        }
    })
},

自动完成 2:

initCAU : function(){
    var selected = 0;
    var suggestions = [];
    var caus;
    var len;
    $("#cauNum").autocomplete({
        source : function(req, add){
            $.getJSON("cauList.do?method=viewCAUListJSON&cauNumber=" + req.term, function(data){
                suggestions = [];

                caus = data[0];
                len = caus.length;

                for(var i = 0; i < len; i++){
                    suggestions.push(caus[i][1].toString());
                };
                add(suggestions);   
            }); //end getjson callback
        }, 

        minLength : 2,

        select : function(){
            thisval = $(this).val();

            for(var i = 0;i < len; i++){

                if(parseInt(thisval,10) === caus[i][1]){
                    $("#cauID").val(caus[i][0]);
                    return;
                }   
            } 
        }
    })
},

I have two jQuery UI auto-completes on the same page and I'd like to make the code more "functional" and terse. My background is almost strictly OO and I'd like to get more serious about writing more functional JavaScript.

Both of these functions are properties of an object literal that I'm using to namespace the functions on the page. Right now there is a lot of code that's repeated between the functions and I'd like to use something similar to the "partial application" pattern to reduce the code.

autocomplete 1:

initProject : function(){
    var selected = 0;
    var suggestions = []; 
    var projs;
    var len;

    $("#projectNum").autocomplete({
        source : function(req, add){
            $.getJSON("projectList.do?method=viewProjectListJSON&contractID=" + req.term, function(data){
                //clear the suggestions array
                suggestions = [];

                projs = data[0];

                len = projs.length;

                for(var i = 0; i < len; i++){
                    suggestions.push(projs[i][1]);
                };
                add(suggestions);   

            });//end getjson callback
        }, 

        minLength : 2,

        select : function(){
            thisval = $(this).val();

            for(var i = 0;i < len; i++){

                if(thisval === projs[i][1]){
                    $("#projectID").val(projs[i][0]);
                    return;
                }   
            }
        }
    })
},

autocomplete 2:

initCAU : function(){
    var selected = 0;
    var suggestions = [];
    var caus;
    var len;
    $("#cauNum").autocomplete({
        source : function(req, add){
            $.getJSON("cauList.do?method=viewCAUListJSON&cauNumber=" + req.term, function(data){
                suggestions = [];

                caus = data[0];
                len = caus.length;

                for(var i = 0; i < len; i++){
                    suggestions.push(caus[i][1].toString());
                };
                add(suggestions);   
            }); //end getjson callback
        }, 

        minLength : 2,

        select : function(){
            thisval = $(this).val();

            for(var i = 0;i < len; i++){

                if(parseInt(thisval,10) === caus[i][1]){
                    $("#cauID").val(caus[i][0]);
                    return;
                }   
            } 
        }
    })
},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别闹i 2024-12-11 01:07:05
//factored-out common code...
var autocompleter = function(urlPrefix, fragment) {
  var selected = 0;
  var suggestions = [];
  var items;
  var len;
  return ({
    minLength: 2,
    source: function(req, add) {
      $.getJSON(urlPrefix + req.term, function(data) {
          suggestions = [];
          items = data[0];
          len = items.length;
          for(var i = 0; i < len; i++) {
            suggestions.push(projs[i][1]);
          };
          add(suggestions);
        }); //end JSON callback
    }, //end source callback
    select: function() {
      var thisVal = $(this).val();
      for(var i = 0; i < len; i++) {
        if(thisVal === items[i][1]) {
          $(fragment).val(items[i][0]);
          return;
        }
      }
    } //end select callback
  });
};

//verbose but clear way to achieve what you were doing before.
var initCAU = function() {
  var attachTo = "#cauNum";
  var urlPrefix = "cauList.do?method=viewCAUListJSON&cauNumber=";
  var fragment = "#cauID";
  $(attachTo).autocomplete(autocompleter(urlPrefix, fragment));
};

var initProject = function() {
  var attachTo = "#projectNum";
  var urlPrefix = "projectList.do?method=viewProjectListJSON&contractID=";
  var fragment = "#projectID";
  $(attachTo).autocomplete(autocompleter(urlPrefix, fragment));
};
//factored-out common code...
var autocompleter = function(urlPrefix, fragment) {
  var selected = 0;
  var suggestions = [];
  var items;
  var len;
  return ({
    minLength: 2,
    source: function(req, add) {
      $.getJSON(urlPrefix + req.term, function(data) {
          suggestions = [];
          items = data[0];
          len = items.length;
          for(var i = 0; i < len; i++) {
            suggestions.push(projs[i][1]);
          };
          add(suggestions);
        }); //end JSON callback
    }, //end source callback
    select: function() {
      var thisVal = $(this).val();
      for(var i = 0; i < len; i++) {
        if(thisVal === items[i][1]) {
          $(fragment).val(items[i][0]);
          return;
        }
      }
    } //end select callback
  });
};

//verbose but clear way to achieve what you were doing before.
var initCAU = function() {
  var attachTo = "#cauNum";
  var urlPrefix = "cauList.do?method=viewCAUListJSON&cauNumber=";
  var fragment = "#cauID";
  $(attachTo).autocomplete(autocompleter(urlPrefix, fragment));
};

var initProject = function() {
  var attachTo = "#projectNum";
  var urlPrefix = "projectList.do?method=viewProjectListJSON&contractID=";
  var fragment = "#projectID";
  $(attachTo).autocomplete(autocompleter(urlPrefix, fragment));
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文