2012-04-21 43 views
1

的設定值,我有鑑於這樣的代碼:獲取和TextField

App.TodoView = Em.View.extend({ 
    labelView: Em.TextField.extend({ 

    }), 
    createNew:function() { 
     console.log(this.labelView.get('value')); 
    } 
}); 

與此模板:

{{#view App.TodoView}}  
    {{view labelView}}  
    {{#view Em.Button target="parentView" action="createNew"}}Add{{/view}}  
{{/view}} 

而且我得到以下錯誤:

Uncaught TypeError: Object (subclass of Ember.TextField) has no method 'get' 

我想也可以使用insertNewLine方法,所以我可以在模板中設置Em.TextField的值。

回答

4

問題是你正在定義一個類並試圖從中獲取value。你想要的是獲得具體實例的value。這可以通過結合LabelView到其然後可以在App.TodoView被檢索,在這種情況下todoLabel一個值的value來實現,見http://jsfiddle.net/pangratz666/PTPsV/

把手

{{#view App.TodoView }} 
    <!-- Bind the value of the LabelView to todoLabel on the App.TodoView --> 
    {{view LabelView valueBinding="todoLabel" }} 
    {{#view Em.Button target="parentView" action="createNew" }}Add{{/view}} 
{{/view}} 

的JavaScript

App.TodoView = Em.View.extend({ 
    LabelView: Em.TextField.extend(), 

    createNew: function(){ 
     var value = this.get('todoLabel'); 
     console.log('le todoLabel', value); 
    } 
});​ 

注意,因爲你要定義一個CL屁股LabelView這是一個約定,寫在大寫,而實例寫在lowerCase。在The Emberist的博客文章中看到一篇關於命名約定的好文章。

此外,要訪問Ember.Object上的財產,您應始終使用get,因此它的編號爲this.get('todoLabel')而不是。


您現在可以實施進一步的方法如insertNewlinecancel - 注意這insertNewline,而不是insertNewLine,看到text_support

結果是這樣的,看到http://jsfiddle.net/pangratz666/9ZLAC/

App.TodoView = Em.View.extend({ 
    LabelView: Em.TextField.extend({ 
     insertNewline: function(){ 
      this.get('parentView').createNew(); 
     },    
     cancel: function(){ 
      this.set('value', ''); 
     } 
    }), 

    createNew: function(){ 
     var value = this.get('todoLabel'); 
     console.log('le todoLabel', value); 
    } 
});​ 
+0

THX了很多,很大的答案!可能是我的問題很愚蠢,但是燼文件太差了。希望他們改進。 – bravedick 2012-04-21 20:01:02

+0

很高興我能幫到你。文件不斷改進。在此之前,SO是一個很好的幫助資源。 – pangratz 2012-04-21 20:15:06

相關問題