meteor - Template.currentData() 和 template.data 什么时候值不同?

标签 meteor

我知道一个是 react 源,而另一个不是。但我认为他们总是会给出相同的值(value)。

然后我在 Telescope 的源码中找到了以下代码:

    var newTerms = Template.currentData().terms; // ⚡ reactive ⚡
    if (!_.isEqual(newTerms, instance.data.terms)) {
      instance.postsLimit.set(instance.data.terms.limit || Settings.get('postsPerPage', 10));
    }

链接:https://github.com/TelescopeJS/Telescope/blob/master/packages/telescope-posts/lib/client/templates/posts_list/posts_list_controller.js#L33

因此,这两个值似乎有时会有所不同。什么时候?

最佳答案

根据 Meteor 的文档,关于 template.data :

This property provides access to the data context at the top level of the template. It is updated each time the template is re-rendered. Access is read-only and non-reactive.



既然我们知道 the current data context is reactive ,因此可以在不重新渲染模板的情况下进行更改(这使得 react 性在 Blaze 上看起来非常流畅),此 if编写语句是为了检查“真实”当前术语(存储在响应式(Reactive) Template.currentData() 中)与我们拥有的“先前”术语 相比是否发生了变化。当前模板上次呈现的时间 . (存储在非响应式(Reactive) template.data 中)

总结一下,这个自动运行的作用是:
  • 任何时候当前数据上下文发生变化...
  • 从所述数据上下文中获取术语
  • 将这些术语与存储在 template.data 中的术语进行比较当模板呈现时
  • 如果它们不同,则意味着条款已更改(废话):重置帖子限制。
  • 关于meteor - Template.currentData() 和 template.data 什么时候值不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32505601/

    相关文章:

    javascript - 我应该使用 Meteor.startup() 还是 $(function() {})

    javascript - 了解 Javascript 编译器,也许还有 Meteor JS

    javascript - 根据react中对象的索引渲染不同的html

    javascript - Meteor:错误:ENOTEMPTY:部署到 Modulus 时目录不为空

    meteor - meteor 中的子域支持(如使用 slack - http ://team. slack.com)

    Meteor 0.9.2 远程连接问题

    arrays - meteor :具有对象数组的自定义AutoForm

    javascript - 从文档中删除对象

    meteor - Meteor 中的 Google Oauth 流程

    javascript - meteor d3.js 通信事件