【问题标题】:Meteor: reactive helper using Tracker.DependencyMeteor:使用 Tracker.Dependency 的响应式助手
【发布时间】:2015-01-29 00:12:10
【问题描述】:

我有一个帮手

Template.home.helpers({
  songId: function(){
    return SongManager.getSongId();
  },
});

使用 SongManager 中的 getSongId 方法

SongManager = {
  init: function(songId){
    this.dep = new Tracker.Dependency;
  },
  getSongId: function(){
    this.dep.depend();
    return this.songId;
  },
  setSongId: function(arg){
    this.songId = arg;
    this.dep.changed();
  },
}

但它不会在调用 setSongId 时导致模板响应式更新。我做错什么了吗?如果我使用 Session.get 和 Session.set,那么一切正常。

SongManager = {
  init: function(songId){
  },
  getSongId: function(){
    return Session.get('songId');
  },
  setSongId: function(arg){
    Session.set('songId', arg);
  },
}

【问题讨论】:

    标签: meteor helpers


    【解决方案1】:

    我解决了它,但我很惊讶,但结果:放置在 Meteor.startup() 内不起作用。我需要调查一下为什么,Blaze 有一些我还不知道的东西。

    检查我为此创建的存储库:https://github.com/mariorodriguespt/stack-song-manager

    【讨论】:

    • 对不起,我没有显示初始化代码,因为我把它放在了 Meteor.startup 中。我实际上根本不想使用 Session,我只是将它添加到那里,因为我没有 Session 的原始代码不起作用(请参阅我的编辑)。
    • 有趣。不知何故,这也有效:SongManager = {dep: new Tracker.Dependency, init: ...}
    • 之所以有效,是因为init 方法中的this 是对SongManager 对象的引用。
    猜你喜欢
    • 2015-03-11
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2015-12-01
    相关资源
    最近更新 更多