[英]Best way to store “temp” data on an ember button
I have an Ember.Button which should remove an element from an array. 我有一个Ember.Button应该从数组中删除一个元素。 The button label is simply an X (for now).
按钮标签只是一个X(现在)。
I'd like to know the best way to store data for use by the button. 我想知道存储数据以供按钮使用的最佳方法。 If this was plain jquery I might use data-username.
如果这是简单的jQuery,我可能会使用数据用户名。 But what's the best way to do this?
但是最好的方法是什么?
update Use case for this question would be something like this: 更新此问题的用例将是这样的:
{{#each App.recentUsersArray}}
<li>
{{#view App.RecentNameBtn contentBinding="this"}} {{content}} {{/view}}
{{#view App.RecentNameDeleteBtn}}X{{/view}}
</li>
{{/each}}
In the second view, I need a way to know which username the delete action should apply to. 在第二个视图中,我需要一种方法来知道删除操作应应用于哪个用户名。
Use the {{action}}
helper, which passes the context as argument, see http://jsfiddle.net/zVd9g/ . 使用
{{action}}
帮助程序,该辅助程序将上下文作为参数传递,请参见http://jsfiddle.net/zVd9g/ 。 Note: in the upcoming Ember.js version the action helper only passes one argument so you would have to adapt your sources accordingly . 注:在即将到来的Ember.js版动作助手只传递一个参数,因此你必须去适应你的源代码相应 。
If you want to use your existing views, you could do a contentBinding="this"
on the App.RecentNameDeleteBtn
as you already did on the App.RecentNameBtn
. 如果要使用现有的观点,你可以做一个
contentBinding="this"
的App.RecentNameDeleteBtn
因为你已经在做了App.RecentNameBtn
。
Handlebars: 车把
<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: 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));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.