2017-06-08 21 views
-2

ここでコードは冗長コードを削除し、コードを別の機能に移動しようとしています。冗長コードを削除して別のJqueryファンクションに移動したい

 //Adding Infotypes to filter and checks whether any infotype option is selected 
     if(this.$infoOptions.val() != null){ 
      var infotypelength = this.$infoOptions.val().length; 
      var l=0,j; 
      //Condition to check empty input type 
      if(infotypelength > 0){ 
       var infotypeList = this.$infoOptions.val(); 
       for(j = 0; j < infotypelength; j++) { 
       //It checks whether already option is selected and prevents adding to filter if its duplicate. 
       if(($.inArray($('#infoOptions').select2('data')[j].text, filterList)) == -1){ 
        this.filter.push($('#infoOptions').select2('data')[j].text); 
        if(infotypeList[j].contains('_subgroup')){ 
        var res = infotypeList[j].split("_"); 
        this.aSubinfotype[l]=res[0]; 
        l=l+1; 
        } 
        else 
        this.aInfotypes.push(infotypeList[j]); 
       } 
       } 
      } 
     } 

     //Adding Countries to filter 
     if(this.$countryOptions.val() != null){ 
      var geoLength = this.$countryOptions.val().length; 
      //Condition to check empty input type 
      if(geoLength > 0){ 
       var geoList = this.$countryOptions.val(); 
       var l=0; 
       for(var j = 0; j < geoLength; j++) { 
       if(($.inArray($('#countryOptions').select2('data')[j].text, filterList)) == -1){ 
        this.filter.push($('#countryOptions').select2('data')[j].text); 
        if(geoList[j].contains('_subgroup')){ 
         var res = geoList[j].split("_"); 
         this.aSubgeotype[l]=res[0]; 
         l=l+1; 
        } 
        else 
        this.aGeography.push(geoList[j]); 
       } 
       } 
      } 
     } 

しかし、変数とキャッシュされたセレクタを他の関数に渡す際に問題に直面しています。誰もこれで私を助けることができますか?

+1

あなたはそれを行うために時間のヘルプは必要ありません...両方のブロックの間で異なるデータを抽出し、これらのデータをパラメータに使って関数を作成してください... – Kashkain

+0

返信いただきありがとうございます。キャッシュされたセレクタ($( '#infoOptions')、select2( 'data'))を引数として関数に渡すにはどうしたらいいですか?値をグローバル変数にプッシュしたいのですが、ローカルスコープの配列ではありません。いくつかの例で私を助けてください。 –

答えて

0

私はあなたの実装をどのように行われるかわかりませんが、私は本当にあなたがそれを改善することができると思い、方法によっては、別の双方向ビットでコードを減らすことができます

var myFunction = function(option, filter, array, selector, subType) { 
    if(option && option.val()){ 
     var optList = option.val(); 
     var optLength = optList.length; 
     //Condition to check empty input type 
     if(optLength > 0) { 
      var l = 0; 
      for(var j = 0; j < optLength; j++) { 
       if(($.inArray(selector.select2('data')[j].text, filterList)) == -1){ 
        filter.push(selector.select2('data')[j].text); 
        if(optList[j].contains('_subgroup')){ 
         var res = optList[j].split("_"); 
         subType[l]=res[0]; 
         l=l+1; 
        } else { 
         array.push(optList[j]); 
        } 
       } 
      } 
     } 
    } 
} 

はコール:myFunction(this.$countryOptions, this.filter, this.aGeography, $('#countryOptions'), this.aSubgeotype)

// data = {option, filter, array, selector, subType} 
var myFunction = function(data) { 
    if(data.option && data.option.val()){ 
     var optList = data.option.val(); 
     var optLength = optList.length; 
     //Condition to check empty input type 
     if(optLength > 0) { 
      var l = 0; 
      for(var j = 0; j < optLength; j++) { 
       if(($.inArray(data.selector.select2('data')[j].text, filterList)) == -1){ 
        data.filter.push(data.selector.select2('data')[j].text); 
        if(optList[j].contains('_subgroup')){ 
         var res = optList[j].split("_"); 
         data.subType[l]=res[0]; 
         l=l+1; 
        } else { 
         data.array.push(optList[j]); 
        } 
       } 
      } 
     } 
    } 
} 

コール:

myFunction({ 
    option: this.$countryOptions, 
    filter: this.filter, 
    array: this.aGeography, 
    selector: $('#countryOptions'), 
    subType: this.aSubgeotype 
}); 

または

var data = { 
    option: this.$countryOptions, 
    filter: this.filter, 
    array: this.aGeography, 
    selector: $('#countryOptions'), 
    subType: this.aSubgeotype 
} 
myFunction(data); 

第1の方法は、データを1つずつ渡すことです。次に、データをjsonオブジェクトに渡します。

+0

ありがとう!これは確かにさらに進むために役立ちます..もう一度ありがとう! –

+0

あなたは歓迎です、あなたは私の答えを検証することができます:) – Kashkain

関連する問題