2014-11-05 51 views
1

我有一个函数,要求为用户给出的每个单词进行聚合,然后绘制图表。将参数传递给elasticsearch javascript api搜索请求的回调函数

我想在回调中知道发送请求时,我的循环变量i的值是什么。

如何在弹性搜索API修复的预定义参数中传递变量i?

for(var i = 0; i < 15; i++) 
{ 
    client.search({ 
     index: 'twitter', 
     type: "status", 
     size : 10, 
     body: 
     { 
      query: { 
       "bool": { 
        "must": [ 
         {"query_string": { 
          "fields" : ["text"], 
          "default_operator" : "AND", 
          "query" : $scope.motsCompares[i] 
         }}, 
         {"range": { 
          "created_at": { 
           "gte": moment().subtract(duration, key).format("YYYY-MM-DD") 
          } 
         }} 
        ] 
       } 
      }, 
      aggs : { 
       "frequence_mots" : { 
        "date_histogram" : { 
         "field" : "created_at", 
         "interval" : "day", 
         "format" : "dd/MM/yyyy", 
         "min_doc_count" : 0 
        } 
       } 
      } 
     } 
    }).then(function traiterResultat(body) { 

     // I would like to use i from the loop here to get the right word in my array ($scope.motsCompares[i]) 

    }, function (error) { 
      console.trace(error.message); 
    }); 
} 

回答

1

fn.bind()出现之前,这样的事情需要关闭或其他方法太可怕了,我会甚至没有提到他们。

由于ECMAScript 5,您可以利用fn.bind()的“currying”特性立即通过i并在以后通过body,此时承诺链将其成功路径拉下。

for(var i = 0; i < 15; i++) { 
    (function(i) { 
     client.search({ 
      // ... 
     }).then(function (i, body) { 
      //.then()'s callback is an intermediate function returned by .bind(). 
      // `i` here is the loop's `i` that was bound-in by .bind(). 
      //`body` is passed to the intermediate function later, when the promise chain rips down its success path. 
     }.bind(null, i), function (error) { 
      console.trace(error.message); 
     }); 
    })(i); 
} 

您可以更改.bind(null, i)null你想成为this回调内的任何物体。

1

使用另一个函数来创建你的回调:

for(var i = 0; i < 15; i++) 
{ 
    client.search({ 
        index: 'twitter', 
        type: "status", 
        size : 10, 
        body: 
        { 
        query: { 
         "bool": { 
         "must": [ 
          {"query_string": { 
          "fields" : ["text"], 
          "default_operator" : "AND", 
          "query" : $scope.motsCompares[i] 
          }}, 
          {"range": { 
          "created_at": { 
           "gte": moment().subtract(duration, key).format("YYYY-MM-DD") 
          } 
          }} 
         ] 
         } 
        }, 
        aggs : { 
         "frequence_mots" : { 
         "date_histogram" : { 
          "field" : "created_at", 
          "interval" : "day", 
          "format" : "dd/MM/yyyy", 
          "min_doc_count" : 0 
         } 
         } 
        } 
        } 
       }).then(createCallback(i), function (error) { 
          console.trace(error.message); 
         }); 
} 

function createCallback(i){ 
    return function traiterResultat(body) { 

    // use i from the loop here to get the right word in my array ($scope.motsCompares[i]) 

    } 
} 
+0

它的工作原理!谢谢。因此,回调函数可以访问其直接父级的上下文,那是这个想法吗? – 2014-11-05 15:56:26

+1

@Ganoninc如果他解决了你的问题 - 考虑接受这个答案。 – 2014-11-05 16:38:46

+0

是的,接受答案:)。要回答你的第二个问题 - 每个新功能创建一个新的范围。并且来自外部作用域的任何变量都可以在内部作用域中“关闭”。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures – septerr 2014-11-05 21:53:50

相关问题