前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序之SelectorQuery

微信小程序之SelectorQuery

作者头像
IMWeb前端团队
发布2019-12-04 16:47:22
3.6K0
发布2019-12-04 16:47:22
举报
文章被收录于专栏:IMWeb前端团队IMWeb前端团队

本文作者:IMWeb howenhuo 原文出处:IMWeb社区 未经同意,禁止转载

在开发小程序展开全文组件时需要用到节点查询API - wx.createSelectorQuery() 来查询全文内容的高度。

  • wx.createSelectorQuery() 返回一个 SelectorQuery 对象实例。
  • SelectorQuery 有五个方法(inselectselectAllselectViewportexec),第一个返回 SelectorQuery,后四个返回 NodesRef
  • NodesRef 有四个方法(fieldsboundingClientRectscrollOffsetcontext),第一个返回 NodesRef,后三个返回 SelectorQuery

对照官方提供的示例代码来看

代码语言:javascript
复制
const query = wx.createSelectorQuery() // query 是 SelectorQuery 对象
query.select('#the-id').boundingClientRect() // select 后是 NodesRef 对象,然后 boundingClientRect 返回 SelectorQuery 对象
query.selectViewport().scrollOffset() // selectViewport 后是 NodesRef 对象,然后 scrollOffset 返回 SelectorQuery 对象
query.exec(function (res) { // exec 返回 NodesRef 对象
  res[0].top // #the-id节点的上边界坐标
  res[1].scrollTop // 显示区域的竖直滚动位置
})

问题:每行执行返回的 SelectorQuery 对象是相同的吗?

答案:是的,都是同一个对象。

问题:直接执行 query.select('#the-id').boundingClientRect().exec 也可以吗?

答案:可以,boundingClientRect() 返回就是 query

问题:这样连写 query.select('#the-id').boundingClientRect().selectViewport().scrollOffset() 算两条查询请求吗?

答案:是两条请求。

问题:query.exec 执行后会清空前面的查询请求吗?再次执行还能拿到结果吗?

答案:可以,query 不会清空请求。

问题:boundingClientRectscrollOffset 可以接受 callback 参数,它与 query.exec 执行顺序是怎样,修改 res 结果会影响到后面的 callback 吗?

答案:先执行 boundingClientRectscrollOffsetcallback,再执行 query.execcallback;修改 res 结果会影响到后面 exec 的结果。

上面的问题通过小程序开发者工具中的 WAService.js 源码简单美化还原后可以了解 SelectorQuery 的代码逻辑

SelectorQuery.js

代码语言:javascript
复制
import NodesRef from 'NodesRef';
import requestComponentInfo from 'requestComponentInfo';

export default function(pluginId) {
  return class SelectorQuery {
    constructor(plugin) {
      if (plugin && plugin.page) {
        this._component = this._defaultComponent = plugin.page;
        this._webviewId = this._defaultComponent.__wxWebviewId__;
      } else {
        var pages = __internalGlobal__.getCurrentPagesByDomain('');
        this._defaultComponent = pages[pages.length - 1],
        this._component = null;
        this._webviewId = null;
      }
      this._queue = [];
      this._queueCb = [];
    }
    in(component) {
      if (!this._webviewId) {
        this._webviewId = component.__wxWebviewId__;
        this._component = component;
      } else if (this._webviewId !== component.__wxWebviewId__) {
        console.error('A single SelectorQuery could not work in components in different pages. A SelectorQuery#in call has been ignored.');
      } else {
        this._component = component;
      }
      return this;
    }
    select(selector) {
      return new NodesRef(this, this._component, selector, true);
    }
    selectAll(selector) {
      return new NodesRef(this, this._component, selector, false);
    }
    selectViewport() {
      return new NodesRef(this, 0, '', true);
    }
    _push(selector, component, single, fields, callback) {
      if (!this._webviewId) {
        this._webviewId = this._defaultComponent ? this._defaultComponent.__wxWebviewId__ : undefined;
      }
      const rootNodeId = pluginId ? '' : r.getRootNodeId(this._webviewId);
      this._queue.push({
        component: null != component ? (0 === component ? 0 : component.__wxExparserNodeId__) : rootNodeId,
        selector,
        single,
        fields,
      });
      this._queueCb.push(callback || null);
    }
    exec(callback) {
      requestComponentInfo(this._webviewId, {
        pluginId,
        queue: this._queue,
      }, (results) => {
        const queueCb = this._queueCb;
        results.forEach((res, index) => {
          if ('function' == typeof queueCb[index]) {
            queueCb[index].call(this, res);
          }
        });
        if ('function' == typeof callback) {
          callback.call(this, results);
        }
      })
    }
  }
}

requestComponentInfo.js

代码语言:javascript
复制
const subscribe = function(eventType, callback) {
  const _callback = function(event, webviewId, nativeInfo = {}) {
    const { data = {}, options } = event;
    const startTime = options && options.timestamp || 0;
    const endTime = Date.now();
    if ('function' == typeof callback) {
      callback(data, webviewId);
      Reporter.speedReport({
        key: 'webview2AppService',
        data,
        timeMark: {
          startTime,
          endTime,
          nativeTime: nativeInfo.nativeTime || 0,
        }
      });
    }
  };
  __safeway__.bridge.subscribe(eventType, _callback);
}

const publish = function(eventType, data, webviewIds) {
  const event = {
    data,
    options: {
      timestamp: Date.now(),
    }
  };
  __safeway__.bridge.publish(eventType, event, webviewIds);
}

const requestCb = {};
let requestId = 1;
subscribe('responseComponentInfo', function(data) {
  const reqId = data.reqId;
  const callback = requestCb[reqId];
  if (callback) {
    delete requestCb[reqId];
    callback(data.res);
  }
});
export default function requestComponentInfo(webviewId, reqs, callback) {
  const reqId = requestId++;
  if (!webviewId) {
    console.warn('An SelectorQuery call is ignored because no proper page or component is found. Please considering using `SelectorQuery.in` to specify a proper one.');
    return;
  }
  requestCb[reqId] = callback,
  publish('requestComponentInfo',
    {
      reqId,
      reqs,
    }, 
    [webviewId],
  );
}

NodesRef.js

代码语言:javascript
复制
export default class NodesRef {
  constructor(selectorQuery, component, selector, single) {
    this._selectorQuery = selectorQuery;
    this._component = component;
    this._selector = selector;
    this._single = single;
  }
  fields(fields, callback) {
    this._selectorQuery._push(this._selector, this._component, this._single,
      fields,
      callback,
    );
    return this._selectorQuery;
  }
  boundingClientRect(callback) {
    this._selectorQuery._push( this._selector, this._component, this._single,
      {
        id: true,
        dataset: true,
        rect: true,
        size: true,
      },
      callback,
    );
    return this._selectorQuery;
  }
  scrollOffset(callback) {
    this._selectorQuery._push( this._selector, this._component, this._single,
      {
        id: true,
        dataset: true,
        scrollOffset: true,
      },
      callback,
    );
    return this._selectorQuery;
  }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档