【问题标题】:Meteor - data fetched multiple times?流星 - 多次获取数据?
【发布时间】:2015-07-22 15:32:05
【问题描述】:

我正在使用 Meteor Admin 项目存根 (https://github.com/yogiben/meteor-admin)。

我修改了我的数据 - main.coffee 中的 posts 集合以包含在 buildPostSearch 函数中定义的一些自定义过滤:

Router.map ->

    //cut 

    @route "dashboard",
        path: "/dashboard"
        waitOn: ->
          [
            subs.subscribe 'posts'
          ]
        data: ->
            posts: Posts.find( buildPostSearch() ).fetch()

buildPostSearch = () ->
    console.log "Executed."
    { //filter object constructed depending on Session parameters }

这可以正常工作,但在页面刷新时会被多次调用。我可以在浏览器控制台中看到:

Executed.
Executed.
Executed.
Executed.
Executed.
Executed.
(...around 50 times)

我担心性能。它是否多次查询数据库?有没有更好的办法?

【问题讨论】:

  • 你确定waitOn不是原因吗?我的意思是它循环检查订阅,它不会每次都运行它

标签: meteor coffeescript iron-router meteor-collection2


【解决方案1】:

data 钩子是反应性的,所以它触发 multiple times 是完全正常的。

重要的是要记住,当它运行时,它会从您的本地 minimongo 缓存而不是从实际数据库中获取文档。这些find 操作中的每一个实际上都花费了极少的时间,因此性能不是问题。

至于为什么会运行这么多次,我怀疑可能与buildPostSearch 的性质有关。正如您在 cmets 中所指出的,buildPostSearch 取决于会话变量,因此每次其中一个变量发生变化时,您的 data 挂钩都会再次执行。

附加说明:我认为您的示例代码中的意思是 data: -> 而不是 data ->

【讨论】:

  • 感谢您的回复。你是对的,有data: ->
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多