简体   繁体   English

为什么我的使用session.get的模板助手不起作用?

[英]Why is my template helper that uses session.get not reactive?

In my client code I have a Session which is set to an object in a global function: 在我的客户代码中,我有一个Session,该Session设置为全局函数中的一个对象:

somefunctions.js somefunctions.js

updateTimelineItem = function(newSelection){
    var selectedItem = $.grep(Session.get('liveProjectData').items,function(e){return e.position ==newSelection.parent().index()});
    Session.set('selectedItem',selectedItem[0]);
};

However, in a template file where I need to display portions of this session's object data, my helper does not fire after the session is set. 但是,在我需要显示该会话的部分对象数据的模板文件中,设置会话之后,我的助手不会启动。

mytemplate.js mytemplate.js

Template.mytemplate.helpers({    
    selectedItem: function(){
        console.log('reactive update. new item selected.');
        return Session.get('selectedItem');
    }
})

Example of what the session stores 会话存储内容的示例

Object { position: 0, type: "image", source: "imgur", source-url: "https://dl.dropboxusercontent.com/u…", provider: "magic", animation: "puff", thumb: "https://dl.dropboxusercontent.com/u…", fullsize: "https://dl.dropboxusercontent.com/u…", duration: 1000 }

I have tried to find documentation regarding when a Session would not be reactive without much luck. 我试图找到有关何时没有好运就没有反应的文档。 I know the session is set because I can write Session.get('selectedItem') in a browser console and I get the expected output. 我知道会话已设置,因为我可以在浏览器控制台中编写Session.get('selectedItem')并得到预期的输出。

Thank you for any help. 感谢您的任何帮助。

The reason my reactive code was not running is because I did not access the specific reactive variable in the spacebars of that template. 我的反应式代码未运行的原因是因为我没有访问该模板的空格键中的特定反应式变量。 Although Template.helpers is a reactive computation, it executes based on usage of reactive vars in the dom. 尽管Template.helpers是反应式计算,但它是根据dom中反应式var的使用来执行的。 This is unlike other reactive computations. 这不同于其他反应式计算。

Information regarding this is hard to come by but it is hinted at in the Meteor documentation for Template.currentData() and its usage inside of helper. 关于此的信息很难获得,但它在Meteor文档中的Template.currentData()及其辅助工具中的用法中得到了提示。

The work I needed to do with the reactive variable was outside of the view and should therefore not be used in Template.helper but instead template.autorun . 我需要对反应变量进行的工作不在视图范围内,因此不应在Template.helper中使用,而应在template.autorun中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM