2013-12-16 29 views
0

我在尋找一個簡單的輸入&通過函數運行它的值,並將該函數的結果顯示在同一個模板的另一個地方。我只是無法弄清楚我在哪裏連接綁定以顯示功能的結果。我試圖對輸入的值進行計算並將結果推送到{{suggestedGrams}}。我爲這個模板使用了生成的視圖。提交輸入時運行函數

下面是我得到了什麼:

模板

<script type="text/x-handlebars" data-template-name="brew"> 
{{input type="text" id="start-brew" placeholder="Enter a number, e.g: 16" name=id value=submittedOunces action="startBrew"}} 

<p>We recommend using {{suggestedGrams}} grams of coffee for {{ounces}} oz. in a {{id}}.</p> 
</script> 

控制器

App.BrewController = Ember.ObjectController.extend({ 
submittedOunces: "", 
// updates the {{ounces}} binding with value typed in input 
ounces: function() { 
    if (this.submittedOunces.length < 1) { 
     return ""; 
    } else { 
     return this.get("submittedOunces"); 
    } 
}.property("submittedOunces"), 
    actions: { 
     startBrew: function() { 
      // other logic is here to show hidden div's 
     } 
    } 
}); 

我想我失去了一些東西很明顯,但我完全新到Ember,所以很難找到合適的條款來找到我要找的東西。任何幫助,將不勝感激。

+0

爲了得到答案,請閱讀本主題:http://stackoverflow.com/questions/18309544/how-do-i-handle-form-submission-in-ember-js – MrBoolean

+0

我不確定你想要做什麼。如果您向您的控制器添加了「SuggestedGrams:」test「',是否顯示?你是否想在某個地方給「建議的格雷姆斯」造成傷害? – claptimes

+0

@claptimes - 是的。對不起,如果我的描述不清楚。爲了簡潔起見,我拿出了一些代碼。基本上我想把輸入的值輸入到輸入中,並運行一些數學運算(例如「輸入的值」* 2)。 – motoxer4533

回答

0

我通過添加另一個函數解決了這個問題,這要歸功於@claptimes(請參閱原始問題的評論)。我遇到的障礙是功能範圍 - 我在一個函數中讀取一個變量,出於某種原因,我期望在另一個函數中可用。這是我的最終代碼:

App.BrewController = Ember.ObjectController.extend({ 
    submittedOunces: "", 
    ounces: function() { 
     if (this.submittedOunces.length < 1) { 
      return ""; 
     } else { 
      return this.get("submittedOunces"); 
     } 
    }.property("submittedOunces"), 
    // This is where I was running into trouble. 
    // I needed to declare the value again to gain access to it. 
    grams: function() { 
     var submittedOunces = this.get("submittedOunces"); 
     return submittedOunces * 2; // or whatever the function (in my case, a simple math formula) needs to be 
    }.property("submittedOunces"), 
    actions: { 
     startBrew: function() {  
      $('.hide').fadeIn("fast");  
     } 
    } 
});