2012-03-17 32 views
0

私は配列から要素を削除するEmber.Buttonを持っています。ボタンのラベルは単にX(今のところ)です。emberボタンに "temp"データを保存する最も良い方法

ボタンで使用するためにデータを保存する最善の方法を知りたいと思います。これが普通のjqueryの場合、私はdata-usernameを使用するかもしれません。しかし、これを行う最良の方法は何ですか?この質問のための

更新 ユースケースは、このようなものになるだろう:

{{#each App.recentUsersArray}} 
    <li> 
     {{#view App.RecentNameBtn contentBinding="this"}} {{content}} {{/view}} 
     {{#view App.RecentNameDeleteBtn}}X{{/view}} 
    </li> 
{{/each}} 

2番目のビューでは、私はに適用する削除アクションをユーザ名を知るための方法が必要です。

+0

あなたは、私は私の答えを更新しました、クライアント側 –

+0

のセッションのようなものを探しています。 – pangratz

答えて

1

コンテキストを引数として渡す{{action}}ヘルパーを使用してください(http://jsfiddle.net/zVd9g/を参照)。注:今後のEmber.jsバージョンでは、アクションヘルパーは引数を1つだけ渡すので、ソースを変更する必要がありますaccordingly

既存のビューを使用する場合は、すでにApp.RecentNameBtnのようにApp.RecentNameDeleteBtncontentBinding="this"を追加することができます。

ハンドルバー:

<script type="text/x-handlebars" > 
    {{#each App.arrayController}} 
     <button {{action "showTweets" target="App.arrayController" }} >{{this}}</button> 
     <button {{action "removeItem" target="App.arrayController" }}>x</button> 
     <br/>    
    {{/each}} 
</script>​ 

はJavaScript:

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

App.arrayController = Ember.ArrayProxy.create({ 
    content: [1, 2, 3, 4, 5, 6], 
    removeItem: function(view, evt, item) { 
     console.log('remove user %@'.fmt(item)); 
     this.removeObject(item); 
    }, 
    showTweets: function(view, evt, item) { 
     console.log('show tweets of user %@'.fmt(item)); 
    } 
});​ 
+0

質問のいくつか。 1).fmtとはこれはあなたのconsole.logステートメントの%@へのバインディングですか?それはどこに文書化されていますか? 2)アクション "showTweets"。これは、項目を含むオブジェクトのメソッドを正しく呼び出すだけですか? – commadelimited

+0

ところで、これは間違いなくボタンを使うよりも優れています。私はそのアクションがループ内の現在のアイテムを自動的に渡したことが大好きです。 – commadelimited

+1

'fmt'は文字列を書式設定するヘルパーです。私のブログの投稿http://code418.com/blog/2012/03/08/useful-emberjs-functions/を見て、コードを見てください。https://github.com/emberjs/ember.js/blob/3b60016c2b6b38646d954014d10f74fcfe1a3d54 /packages/ember-runtime/lib/system/string.js#L49-57 – pangratz

関連する問題