2015-12-14 57 views
3

我一直試圖通過使用select標記獲取貨幣兌換更新並使用JQuery獲取值。流星貨幣交換

我原來的計劃是使用來自流星把手的{{#if}}來做邏輯。在使用MongoDB切換字段時,當用戶點擊不同的選項時,它會自動切換貨幣字段。

我目前正在使用名爲theara:moneyjs的流星包。請點擊here瞭解包裝信息。

這裏是我當前的代碼,我有:

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"); 
      } 
    } 
)}; 

任何幫助,將不勝感激。

+0

你在發佈你需要的數據嗎?我假設你在客戶端執行此操作時,需要從客戶端數據存儲(minimongo)獲取數據,其次應該使用事件。你做過流星教程的介紹嗎? HTTPS://www.meteor。com/tutorials/blaze/creating-an-app流星在方法上有很大的不同,你將會遇到這樣的困難時刻。 – pushplaybang

+0

對不起,延遲迴復,是的,我正在發佈和訂閱我的數據。我的發佈是在服務器端完成的,而訂閱則在客戶端完成,在同一個JS文件中完成。 –

回答

0

getEXR必須返回一個布爾值。 這不像在Javascript中,大火不要評價這一點。你所要做的EN幫手至極返回一個布爾

0

正如我在評論中提及了你應該跟隨入門教程,但這裏的關鍵組成部分是,你將不得不

  1. 確保你需要將數據從服務器發佈到客戶端
  2. 時創建此模板
  3. 使用模板事件你訂閱的刊物
  4. 你可能會想設置一個反應VAR或會話時變種選擇框變化和t母雞在你的幫手中改變基於該反應性數據源的值。
  5. 使用模板片段#each內和輔助附加到(很容易讓你訪問當前產品放在你的助手)
1

您還需要從getEXR助手返回一個單獨的值,而不是一個遊標。再加上你的幫手沒有返回任何東西!

你甚至不需要布爾值。改用Session變量(或反應性變量)。請看下圖:

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會話變量。