简体   繁体   English

致敬.js中这个回调参数是什么意思?

[英]What's the meaning of this callback parameter in tribute.js?

New to js. js 新手。 I am developing a personal site which uses tribute.js to have an @mention feature.我正在开发使用个人网站tribute.js有一个@mention功能。 In my case, I need to retrieve the mention list from a remote server.就我而言,我需要从远程服务器检索提及列表。 The official document gives an example to implement it.官方文档给出了一个例子来实现。 The thing confusing me is the meaning of cb parameter which is not even defined in anywhere.让我困惑的是cb参数的含义,它甚至没有在任何地方定义。 Could anyone help to explain it?有人可以帮忙解释一下吗?

{
  //..other config options
  // function retrieving an array of objects
  values: function (text, cb) { 
    remoteSearch(text, users => cb(users));
  },
  lookup: 'name',
  fillAttr: 'name'
}
// ajax
function remoteSearch(text, cb) {
  var URL = "YOUR DATA ENDPOINT";
  xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);
        cb(data);
      } else if (xhr.status === 403) {
        cb([]);
      }
    }
  };
  xhr.open("GET", URL + "?q=" + text, true);
  xhr.send();
}

Meaning is hiding in the title of your question, cb means callback :)含义隐藏在您的问题标题中, cb表示回调:)

It gets called by the remoteSearch function which in its turn was called with cb parameter passed by the tribute.js engine.它被remoteSearch函数调用,而该函数又被调用,并带有由tribut.js 引擎传递的cb参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM