流星帮手功能,羊羔和词汇这个 [英] meteor helper functions, lambdas and lexical this

查看:135
本文介绍了流星帮手功能,羊羔和词汇这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

g'day,
我读了流星是全部ecmascript 6 - 并且认为真棒...我从来没有必须再写功能 - 所以很快就把一堆功能改成了lambdas ..只有发现它不起作用(

g'day, I read meteor was going all ecmascript 6 - and thought awesome... "I never have to write 'function' again" - so quickly changed a bunch of functions over to lambdas... only to discover it doesn't work :(

如果你在流星中写一个帮助函数 - 你会得到在this属性中传递的数据上下文,但是当然,lambdas使用一个词汇,所以我很简单地理解这个问题。

If you write a helper function in meteor - you get the data context passed in in the "this" property - but of course, lambdas use a lexical this - so I understand the problem pretty simply.

事情是 - 对我来说并不明显的是解决方案 - 任何想法如何会使用需要当前数据上下文的()=>符号来做一个帮助函数?似乎并不存在于this之外的任何地方?

the thing is - what is not obvious to me is the solution - any idea how you would make a helper function using the () => notation that needs the current data context? It doesn't seem to live anywhere other than "this"?

推荐答案

使用缩写定义函数作为对象属性:

Use shorthand for defining functions as object properties:

Template.someTemplate.helpers({
  someHelper() {
    console.log(this);
  }
});

但是,如果你真的想使用()=> 语法,您可能有兴趣使用 Template.currentData()而不是

But if you really want to use () => syntax, you may be interested in using Template.currentData() instead of this:

Template.someTemplate.helpers({
  someHelper: () => {
    console.log(Template.currentData());
  }
});

文档


Template.currentData()

在帮助器中,返回使用
帮助器的DOM节点的数据上下文。

Inside a helper, returns the data context of the DOM node where the helper was used.

这篇关于流星帮手功能,羊羔和词汇这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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