【问题标题】:How do I bind to bootstrap's modal events in my ember view?如何在我的 ember 视图中绑定到引导程序的模式事件?
【发布时间】:2013-08-05 17:47:45
【问题描述】:

我正在使用 ember 视图来呈现我的应用程序的模式介绍,目前它看起来像这样:

views/modal.js

App.ModalView = Ember.View.extend({
    tagName: 'div',
    classNames: ['modal', 'fade'],
    templateName: 'modal',
    didInsertElement: function() {
        this.$().modal('show');
    }
});

controller/application.js

App.ApplicationController = Ember.ArrayController.extend({
    showModal: function() {
        var modal = App.ModalView.create();
        modal.append();
    }
});

modal.hbs 模板只是一些样板 html。

当我触发 showModal 函数时,我可以很好地显示模态,但是在模态关闭后我无法从 DOM 中删除视图,我正在尝试绑定到 hidden 事件但是我不知道怎么做,谁能指出我正确的方向?

【问题讨论】:

  • 你看过这个 ember 插件了吗:github.com/emberjs-addons/ember-bootstrap 它还包括很好地集成到 ember 中的模式窗格。
  • 这是一个不错的选择,但是我正在尝试学习 Ember,所以很高兴知道我做错了什么以供参考

标签: javascript ember.js modal-dialog bootstrap-modal


【解决方案1】:

我在现有应用中使用过这种方法,效果很好

App.ModalView = Ember.View.extend({
    templateName: "modal",
    title: "",
    content: "",
    classNames: ["modal", "fade", "hide"],
    didInsertElement: function() {
        this.$().modal("show");
        this.$().one("hidden", this._viewDidHide);
    },
    // modal dismissed by example clicked in X, make sure the modal view is destroyed
    _viewDidHide: function() {
        if (!this.isDestroyed) {
            this.destroy();
        }
    },
    // here we click in close button so _viewDidHide is called
    close: function() {        
        this.$(".close").click();
    }
});

这里我们监听hidden事件,当modal完全隐藏时触发,销毁对象。这很重要,因为以编程方式创建的调用view.append() 的视图不会被破坏。这种方法可以确保它。

这是jsfiddle的例子。

【讨论】:

  • 糟糕的是,我使用的是新的引导程序 3.0,他们将事件更改为 hidden.bs.modal。顺便说一句,在您的示例中,它应该是 this.remove() 而不是 this.destroy() 无论如何谢谢!
  • destroy 方法属于 ember,用于从 dom 中移除视图,移除观察者等。不客气@javiervd
  • 我明白了,当我尝试实现它时出现Object #<HTMLDivElement> has no method 'destroy' 错误,虽然切换到remove 工作正常
  • 为了学习ember,能否解释一下使用$('.myModal')this.$()作为选择器附加_viewDidHide方法的区别?
  • 当然。 $('.myModal') 是绑定到整个文档的 jquery 选择器。 view.$('.myModal') 是 $('.myModal', viewContext) 的一种别名,其中 viewContext 是视图的生成 dom。因此使用this.$() 将始终在视图子节点中搜索,有关详细信息,请参阅doc
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2014-02-04
  • 2017-01-16
  • 1970-01-01
  • 2014-11-08
  • 2017-05-22
  • 2012-01-05
相关资源
最近更新 更多