attributes - ember,如何在多个链接中获取 "name"属性?

标签 attributes ember.js

我是 Ember 的新手。我有很多链接(即),带有属性“名称”,并且我想在单击其中一个链接时获得此属性。我知道如何仅使用一个和bindAttr,但是对于更多我正在尝试使用此代码,但不起作用。我需要对每个链接使用许多bindAttr???,昨天我做了这个(仅针对一个链接):

<body>

    <div id="templateHere"></div>

            <!--handlebar-->
    <script type="text/x-handlebars" data-template-name="text">
            <h1>Send the message:</h1>              
            <a {{action "clicked" on="click"}} name='link1'>Click me!!</a>
            <a {{action "clicked" on="click"}} name='link2'>click me again</a>
    </script>​





    <script>
            //namespace
            App = Ember.Application.create();

            //define view 
            App.myview = Ember.View.extend({
                            templateName: 'text',
                            name_attribute:'name_buttooooon',               
                            message: '',
                            clicked: function(event) {
                            console.log(jQuery(this).attr('name'));//get attribute name value
                            }                               
            });

            //create view
            App.myview=App.myview.create();

            //insert view in body
            $(function() {
                App.myview.append('#templateHere');
            });

    </script>

</body>

最佳答案

您可以通过使用event.currentTarget来实现这一点。它是jQuery property :

Description: The current DOM element within the event bubbling phase.

This property will typically be equal to the this of the function.

If you are using jQuery.proxy or another form of scope manipulation, this will be equal to whatever context you have provided, not event.currentTarget

我猜 Ember 为事件做了一些特殊的上下文,因为 jQuery 文档说 currentTarget 应该等于 this

所以它在你的代码中看起来像这样:

clicked: function(event) {
    console.log($(event.currentTarget).attr('name'));
}

您可以尝试这个解决方案 in this JSFiddle .

关于attributes - ember,如何在多个链接中获取 "name"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769802/

相关文章:

model-view-controller - EmberJS : Good separation of concerns for Models, 在相当复杂的应用程序中存储、 Controller 、 View ?

javascript - 使用 ember 数据如何单独异步获取子项?

c# - 数据验证属性和自定义资源管理器

r - 通过使用 igraph (R) 组合事件顶点的属性来创建边缘属性

backbone.js - 为什么模型属性会添加到我的 Backbone.Marionette.ItemView 中?

ember.js - 如何将值表单输入选择绑定(bind)到 Controller 中的属性

javascript - 使用 onclick 属性返回 false

c# - 是否有一个属性可以避免 MSTest 中的级联单元测试失败?

javascript - 使用 Ember.js View 渲染集合

ember.js - ember 中删除的记录不会从集合中删除