2011-12-30 12 views
8

ボタンからこのビュー関数insertNewLineをトリガーする方法についてのアドバイスを探しています(下記のビューとテンプレートを参照)。おそらく、このコードを構造化するためのより良い方法があると思います。ご協力いただきありがとうございます。ボタンからテキストフィールドのアクションをトリガーする

// view 
App.SearchView = Ember.TextField.extend({ 
    insertNewline: function() { 
    var value = this.get('value'); 

    if (value) { 
     App.productsController.search(value); 
    } 
    } 
}); 

// template 
<script type="text/x-handlebars"> 
    {{view App.SearchView placeholder="search"}} 
    <button id="search-button" class="btn primary">Search</button> 
</script> 

答えて

12

あなたのTextFieldにミックスインEmber.TargetActionSupportを使用してinsertNewlineが呼び出されたときにtriggerAction()を実行する可能性があります。 http://jsfiddle.net/pangratz666/zc9AA/

ハンドルバーを参照してください:

<script type="text/x-handlebars"> 
    {{view App.SearchView placeholder="search" target="App.searchController" action="search"}} 
    {{#view Ember.Button target="App.searchController" action="search" }} 
     Search 
    {{/view}} 
</script> 

はJavaScript:

App = Ember.Application.create({}); 

App.searchController = Ember.Object.create({ 
    searchText: '', 
    search: function(){ 
     console.log('search for %@'.fmt(this.get('searchText'))); 
    }  
}); 

App.SearchView = Ember.TextField.extend(Ember.TargetActionSupport, { 
    valueBinding: 'App.searchController.searchText', 
    insertNewline: function() { 
     this.triggerAction(); 
    } 
}); 
+0

素晴らしい、ありがとう。私は、App.SearchViewでのみ、App.searchControllerのためのmixinが必要ではないと推測しています。 –

+0

ああ、そうです。私はコードを更新しました。 – pangratz

関連する問題