比较Meteor.subcribe()与this.subscribe()[Meteor + Blaze] [英] Compare Meteor.subcribe() Vs this.subscribe() [Meteor + Blaze]

查看:176
本文介绍了比较Meteor.subcribe()与this.subscribe()[Meteor + Blaze]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个页面/模板,

  1. 仪表板(还包含一些特定于用户的数据).
  2. 用户.

我正在使用带有Blaze模板的Meteor 1.5.着陆页是仪表板.我在两个模板中都使用集合Users的通用订阅.

I am using Meteor 1.5 with Blaze Template. Landing Page is Dashboard. I am using the common subscription for Collection Users in both Templates.

场景1

当我在仪表板 template.onCreated()中使用Meteor.subscribe('Users')并转到 Users 页面时,我看到一些已经订阅的数据从 Dashboard 返回>订阅.

When I use Meteor.subscribe('Users') in Dashboard's template.onCreated() and go to Users page, I see some already subscribed data coming back from Dashboard's subscription.

代码:

Template.DashBoard.onCreated(function(){
    Meteor.subscribe('Users');
});

Template.Users.onCreated(function(){
    Meteor.subscribe('Users');
});

场景2

当我在仪表板 template.onCreated()中使用this.subscribe('Users')并转到 Users 页面时,我得到了一个新鲜的订阅,并且没有数据从结转仪表板的订阅.

When I use this.subscribe('Users') in Dashboard's template.onCreated() and go to Users page, I get a Fresh Subscription happening here and no data carry over from Dashboard's subscription.

代码:

Template.DashBoard.onCreated(function(){
    this.subscribe('Users');
});

Template.Users.onCreated(function(){
    this.subscribe('Users');
});

问题

Meteor.subscribe('Users')this.subscribe('Users')有什么区别?使用this.subscribe('Users')有什么影响?

What is the difference between Meteor.subscribe('Users') and this.subscribe('Users') ? What can be the impact of using this.subscribe('Users') ?

推荐答案

如Meteor文档中所述,当模板实例被销毁时,模板代码中的this.subscribe将自动取消订阅.

As explained in Meteor documentation, this.subscribe within Template code will be automatically unsubscribed when the template instance is destroyed.

如果想要Meteor.subscribe,则需要明确取消订阅.

Whereas Meteor.subscribe needs to be explicitly unsubscribed if you want it to.

选择使用其中一种取决于您的应用程序结构.如果您确定数据仅与给定模板相关,请使用模板范围的订阅,即this.subscribe.

The decision to use one or the other depends on your app structure. If you are sure the data is relevant only for a given template, then use template scoped subscription, i.e. this.subscribe.

如果跨多个页面使用数据,请使用全局"表单,或者将其作用域设置为更高的模板级别(在整个页面上都存在,例如在布局上).

If the data is used across several pages, either use the "global" form, or scoped at a higher template level (one that persists through your pages, e.g. on layout).

这篇关于比较Meteor.subcribe()与this.subscribe()[Meteor + Blaze]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆