简体   繁体   English

在Ember.js中重新创建Bootstrap Dropdown功能

[英]Recreating Bootstrap Dropdown functionality in Ember.js

Edit: Looking for a way to close the dropdown when anything but the button is clicked: http://jsfiddle.net/brennan/s4JTn/ 编辑:寻找一种方法来关闭除了点击按钮之外的任何内容: http//jsfiddle.net/brennan/s4JTn/

I'm looking to (re)create twitter's dropdown menu in my ember app. 我正在寻找(重新)在我的ember应用程序中创建twitter的下拉菜单。 I'm specifically having an issue trying to close the dropdown when anything except the dropdown is clicked. 当点击下拉列表以外的任何内容时,我特意遇到尝试关闭下拉列表的问题。 Ie. IE浏览器。 I'm looking for a way to add an event listener to my app to close my dropdown when the body of the app is clicked. 我正在寻找一种方法来添加一个事件监听器到我的应用程序,以便在点击应用程序的主体时关闭我的下拉列表。

Here's my (child) view. 这是我(孩子)的观点。

categorySelect: Ember.View.extend({
    isOpen: false,
    selected: /* removed for brevity */,
    content:[
        /* removed for brevity */
        /* follows structure: {slug: 1, title: 'title', isActive: true} */
    ],

    dropdownToggle: function(e){
        var open = this.get('isOpen');
        if(open){
            this.set('isOpen', false);
        }else{
            this.set('isOpen', true);
        }
    },
    select: function(e){
        var selected = e.context;

        this.content.setEach('isActive', false);

        this.set('selected', selected);
        selected.set('isActive', true);

        this.set('isOpen', false);
    }
})

and here is my template code... 这是我的模板代码......

{{#view categorySelect tagName="div" class="btn-group" classBinding="isOpen:open"}}
                        <a class="btn dropdown-toggle btn-facebook btn-small" {{action "dropdownToggle" on="click"}} href="#">
                            {{selected.title}}
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu pull-right">
                            {{#each content}}
                            <li {{bindAttr class="isActive:active"}}><a href="#" {{action "select" on="click"}}>{{title}}</a></li>
                            {{/each}}
                        </ul>
                    {{/view}}

I've tried adding event listeners to the body like the following which isn't working. 我已经尝试将事件监听器添加到正文中,如下所示无法正常工作。 Ember seems to stop propagation if I click on any ember views. 如果我点击任何余烬视图,Ember似乎会停止传播。 So while it works if I click directly on the body, it doesn't work if i click on any of my views :( 因此,如果我直接点击身体它是有效的,如果我点击我的任何视图它不起作用:(

didInsertElement: function(){
        var self = this;
        $('body').on('click', function(){
            self.set('isOpen', false);
        });
    },

Looking for help and advice. 寻求帮助和建议。 Also, if any of the above code looks like crap please let me know I'm looking to learn as much as I can. 此外,如果以上代码中的任何一个看起来像垃圾,请让我知道我正在尽可能多地学习。

I have roughly the same setup as you that works by creating an event listener on the body element using didInsertElement as well. 我有大致相同的设置,通过使用didInsertElement在body元素上创建一个事件监听器。

Only difference is I have a e.stopPropagation() call in dropdownToggle: 唯一的区别是我在dropdownToggle中有一个e.stopPropagation()调用:

dropdownToggle: function(e){
    // [removed]
    e.stopPropagation();
},

Without that line, I was unable to keep the dropdown open, as the click on the dropdown propagates to the body and immediately closes it. 没有那条线,我无法保持下拉列表打开,因为下拉列表上的点击传播到正文并立即关闭它。

On my setup, clicks on views do propagate to the body (hence the above code), and clicking on other views does trigger the event listener on the body. 在我的设置中,点击视图会传播到正文(因此上面的代码),并且单击其他视图会触发正文上的事件侦听器。

My guess is you have some code in your other views stopping the propagation to the body? 我的猜测是你在其他视图中有一些代码阻止传播到身体?

Try http://jsfiddle.net/bjaeP/ for an example. 试试http://jsfiddle.net/bjaeP/作为例子。

I have modified your fiddle : http://jsfiddle.net/tV8X6/ 我修改了你的小提琴: http//jsfiddle.net/tV8X6/

This works for me. 这适合我。 Just removed the action helper for 'dropdownToggle' and added data attribute to the a tag, and added 'dropdown' class for your 'categorySelect' view 刚刚删除了'dropdownToggle'的动作助手并将数据属性添加到了a标记,并为'categorySelect'视图添加了'dropdown'类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM