如何在事件的 Meteor 中获取父数据上下文 [英] How to Get Parent Data Context in Meteor on an Event

查看:26
本文介绍了如何在事件的 Meteor 中获取父数据上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的流星抽认卡应用程序.有一系列问题,每个问题包括:文本、正确答案、错误答案.

I'm working on a simple meteor flashcard application. There is a collection of Questions each with: text, right_answer, wrong_answers.

答案被连接到一个数组中并被打乱,然后模板会列出一个问题和可能的答案.当用户单击答案时,如何从事件中获取 JS 中的父数据上下文.

The answers are concatenated into an array and shuffled then the template lays out a question and possible answers. When a user clicks the answer, from the event how do I get the parent data context in the JS.

类似于:

<button type="button" question="{{../_id}}" class="btn btn-default answer">
{{this}} {{#with ../this}}{{this._id}}{{/with}}
</button>

可以在模板中显示父问题 ID,但我该如何正确执行此操作.目标是拥有一个函数来获取事件并将答案与right_answer"进行比较以确保相等,并在它有效时给你一个分数.谢谢!

works to show parent question ID in the template, but how do I do this properly. The goal is to have a function that grabs the event and compares the answer to the "right_answer" for equality and gives you a point if it works. Thanks!

最终想出了这个解决方案,但我并不喜欢它或认为它是正确的:

Eventually came up with this solution, but I don't really like it or think it is correct:

{{each}}
    {{#with ../this}}
        <button type="button" question="{{../_id}}" class="btn btn-default answer">X</span></button>     
    {{/with}}
    {{this}}
{{/each}}

推荐答案

我通常这样做:

Template.myTemplate.answers = function () {
    var self = this;
    // assume that this.answers is a list of possible answers
    return _.map(this.answers, function (answer) {
         return _.extend(answer, {
             questionId: self._id,
         });
    });
}

然后你就可以开始了,在你的模板中你可以做这样的事情:

Then you're good to go and in your template you can do things like:

<template name="myTemplate">
    {{#each answers}}
        <button data-question="questionId">...</button>
    {{/each}}
</template>

顺便说一句:请注意,根据标准,在 html 元素上使用 question 属性是不正确的.您应该始终使用 data- 作为自定义标签的前缀.

BTW: Note, that using question attribute on html element is incorrect according to the standard. You should always prefix your custom tags with data-.

另请注意,如果您像这样将事件附加到模板中:

Also note, that if you attach events to your templates like this:

Template.myTemplate.events({
   'click button': function (event, template) {
       // ...
   },
});

然后在事件回调中您可以访问 this,它表示呈现 button 元素的上下文,以及 template.data 是附加到您的模板实例的数据上下文,因此实际上这或多或少是您的父上下文".

then in the event callback you have access to this, which represents the context where the button element was rendered, as well as template.data which is the data context attached to your template instance, so in fact this is more or less your "parent context".

注意新的模板引擎,即blaze,允许我们在模板中使用点表示法,所以我上面描述的方法不再需要,{{../_id}} 完全没问题.

Note that the new templating engine, i.e. blaze, allows us to use dot notation in templates, so the method I described above is no longer needed and {{../_id}} is totally fine.

这篇关于如何在事件的 Meteor 中获取父数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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