流星货币兑换

3
我正试图使用 select 标签和 JQuery 获取值来更新货币兑换。我的原始计划是使用 Meteor handlebars 中的 {{#if}} 来完成逻辑。当用户点击不同选项时,使用 MongoDB 切换字段,它将自动切换货币字段。我目前正在使用一个名为 theara:moneyjs 的 Meteor 包。点击此处获取包信息。以下是我目前的代码: HTML
<template name="product_table">
    <table>
        <thead>
            <tr>
                <th>Product Name</th>
                <th>Currency
                    <select id="currency">
                        <option value="aud">AUS Dollar</option>
                        <option value="usd">US Dollar</option>
                        <option value="hkd">HK Dollar</option>
                    </select>     
        {{#each product}}
            <tbody>
                <tr>
                    <td>{{productName}}</td>
                    <td>{{productPrice}}</td>
                    <!-- {{#if getEXR}} Does not work, since is not a boolean value
                        <td>{{productPrice}}</td>
                    {{/if}} -->
            </tr>
            </tbody>
        {{/each}}
    </table>
</template>

JavaScript

Template.product_table.helpers({
    product: function() {
        return Products.find({}, {sort:{createdAt:-1}});
    },

    getEXR: function() {
        $(document).on('change', '#currency', function () {
            var getCurrency = $("#currency option:selected").val();

            if (getCurrency === "aud") {
            //I am not quite sure, how grab specific field values from MongoDB
                fx.convert(Products.find().productPrice()).from("USD").to("AUD");
            }

            else if (getCurrency === "usd") {
                fx.convert(Products.find().productPrice()).from("USD").to("USD");
            }

            else if (getCurrency === "hkd") {
                fx.convert(Products.find().productPrice()).from("USD").to("HKD");
            }
    }
)};

非常感谢您的帮助。


你是否发布了所需的数据?我假设由于你正在客户端上执行此操作,因此需要从客户端数据存储(minimongo)获取数据。其次,你应该使用事件。你是否完成了Meteor教程的介绍?https://www.meteor.com/tutorials/blaze/creating-an-app Meteor在其方法上非常不同,如果你以这种方式接近它,你会遇到麻烦。 - bigmadwolf
抱歉回复晚了,是的,我正在发布和订阅我的数据。我的发布是在服务器端完成的,而我的订阅是在客户端完成的,在同一个JS文件中。 - T. Wolf
3个回答

1

您还需要从您的getEXR助手中返回一个单独的值,而不是一个光标。此外,您的助手没有返回任何内容!

您甚至不需要布尔值。改用会话变量(或响应式变量)即可。请参见下面:

HTML

<template name="product_table">
    <table>
        <thead>
            <tr>
                <th>Product Name</th>
                <th>Currency
                    <select id="currency">
                        <option value="AUD">AUS Dollar</option>
                        <option value="USD">US Dollar</option>
                        <option value="HKD">HK Dollar</option>
                    </select>     
        {{#each product}}
            {{> oneProduct}}
        {{/each}}
    </table>
</template>

<template name="oneProduct">
   <tbody>
       <tr>
           <td>{{productName}}</td>
           <td>{{productPrice}}</td>
           <td>{{localPrice}}</td>
       </tr>
   </tbody>
</template>

JavaScript
Template.product_table.helpers({
    product: function() {
        return Products.find({}, {sort:{createdAt:-1}});
    }  
)};

Template.product_table.events({
  'change #currency': function(ev){
      Session.set('currency') = $("#currency option:selected").val();
  }
});

Template.oneProduct.helpers({
    // with a nested template the data context (this) becomes a single product
    localPrice: function() {
        var currency = Session.get('currency');
        return fx.convert(this.productPrice()).from("USD").to(currency);
    }
)};

你可能还想设置默认的货币转换,并在模板的onCreated处理程序中初始化currency会话变量。

0

getEXR必须返回一个布尔值。 这不像在Javascript中,Blaze不会对此进行评估。您必须编写一个返回布尔值的辅助函数。


0

如我在评论中提到的,您应该遵循入门教程,但关键组件是:

  1. 确保所需数据从服务器发布到客户端
  2. 在创建此模板时订阅发布
  3. 使用模板事件
  4. 当选择框更改时,您可能需要设置一个反应变量或会话变量,然后在助手中根据该反应数据源更改值。
  5. 在#each内使用模板片段,并将助手附加到其中(轻松为您提供对助手中当前产品的访问权限)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接