7

我已将我的应用程序设置为使用 ember 路由架构。我的索引页面看起来像这样(为简单起见)

script(type='text/x-handlebars', data-template-name='application')
    div.container
        {{outlet}}

和我这样的 ember 应用程序

window.App = Em.Application.create({
    ApplicationController: Em.Controller.extend(),
    ApplicationView: Ember.View.extend({
    templateName: 'application'
    }),
    Router: Em.Router.extend({
        root: Em.Route.extend({
        doHome: (router, event) ->
            router.transitionTo('home')
        doInbox: (router, event) ->
            router.transitionTo('inbox')
        doInboxModal: (router, event) ->
            $("#inbox").modal "show"
        home: Em.Route.extend({
            route: '/',
            connectOutlets: (router, event) ->
                router.get('applicationController').connectOutlet('home')   
        }),
        inbox:Em.Route.extend({
            route: '/inbox',
            connectOutlets: (router, event) ->
                router.get('applicationController').connectOutlet('inbox')  
        })
    })  
})

我的主页和收件箱工作正常,但首先,我在我的 doInboxModal 中执行 jquery 以显示模式收件箱。如果我想在模态收件箱上有一个按钮以转到实际的收件箱页面,它将无法正常工作。

所以,问题是,如何正确使用带有 ember 路由的 Twitter Bootstrap 模式?

4

3 回答 3

7

当您路由到一个视图时,在 didInsertElement 中调用模态,这将加载模态。假设您希望模态加载到收件箱视图

App.InboxView = Ember.View.extend({
  didInsertElement: function(){
    $("#my-modal").modal("show");
  }
})

您更新的路由器:

window.App = Em.Application.create({
  ApplicationController: Em.Controller.extend(),
  ApplicationView: Ember.View.extend({
    templateName: 'application'
  }),
  Router: Em.Router.extend({
    root: Em.Route.extend({
      doHome: (router, event) ->
        router.transitionTo('home')
      doInbox: (router, event) ->
        router.transitionTo('inbox')
      home: Em.Route.extend({
        route: '/',
        connectOutlets: (router, event) ->
          router.get('applicationController').connectOutlet('home')   
      }),
      inbox:Em.Route.extend({
        route: '/inbox',
        connectOutlets: (router, event) ->
          router.get('applicationController').connectOutlet('inbox')  
      })
  })  
})

希望这可以帮助...


更新的答案
App.InboxView = Ember.View.extend({
  templateName: "inbox",
  addNewEmail: function(){
    $("#my-modal").modal("show");
  },
  cancelNewEmail: function(){
    $("#my-modal").modal("hide");
  }
})

收件箱.handlebars

<div id="inbox-container">
  <!-- 
    YOUR INBOX CONTENT
    The modal declared below wont show up unless invoked
  -->
  <a {{action addNewEmail}}>New Email</a>
  <a {{action cancelNewEmail}}>Cancel</a>
  <div class="modal hide fade in" id="my-modal">
    <!--
      Put your modal content
    -->
  </div>
</div>

这边走:

  • 模态将在按钮单击时显示
  • 后面的景色不会消失
  • 模态将隐藏在取消按钮上
于 2012-09-25T10:31:08.650 回答
5

如果有人想进一步参考,我已经发布了使用 Ember.js 的 Twitter Bootstrap 模态:

http://generali.st/en/site/topics/show/82-modal-forms-in-emberjs

包括完整源代码的工作 JSBin。还有一些干燥表格的策略。IMO,事物的命名也更传统一些。

于 2013-03-17T22:48:44.730 回答
4

我花了一些时间剖析 Discourse 来了解他们是如何做到的。基本上它们有一个单例模式,路由器处理事件以显示和隐藏模式。

以下是有趣的部分:

话语/app/assets/javascripts/discourse/routes/discourse_route.js

  showModal: function(router, name, model) {
    router.controllerFor('modal').set('modalClass', null);
    router.render(name, {into: 'modal', outlet: 'modalBody'});
    var controller = router.controllerFor(name);
    if (controller) {
      if (model) {
        controller.set('model', model);
      }
      controller.set('flashMessage', null);
    }
  }

话语/应用程序/资产/javascripts/话语/路由/application_route.js

  events: {
    editCategory: function(category) {
      Discourse.Route.showModal(router, 'editCategory', category);
      router.controllerFor('editCategory').set('selectedTab', 'general');
  }

话语/app/assets/javascripts/discourse/views/modal/modal_body_view.js

 Discourse.ModalBodyView = Discourse.View.extend({

  // Focus on first element
  didInsertElement: function() {
    $('#discourse-modal').modal('show');

    var controller = this.get('controller');
    $('#discourse-modal').on('hide.discourse', function() {
      controller.send('closeModal');
    });

    $('#modal-alert').hide();

    var modalBodyView = this;
    Em.run.schedule('afterRender', function() {
      modalBodyView.$('input:first').focus();
    });

    var title = this.get('title');
    if (title) {
      this.set('controller.controllers.modal.title', title);
    }
  },

  willDestroyElement: function() {
    $('#discourse-modal').off('hide.discourse');
  }

 });

话语/应用程序/资产/javascripts/话语/mixins/modal_functionality.js

 Discourse.ModalFunctionality = Em.Mixin.create({
  needs: ['modal'],

  /**
    Flash a message at the top of the modal

    @method blank
    @param {String} name the name of the property we want to check
    @return {Boolean}
  **/
  flash: function(message, messageClass) {
    this.set('flashMessage', Em.Object.create({
      message: message,
      messageClass: messageClass
    }));
  }

 });

app/assets/javascripts/discourse/templates/modal/modal.js.handlebars

 <div class="modal-header">
   <a class="close" {{action closeModal}}><i class='icon-remove icon'></i></a>
   <h3>{{title}}</h3>
 </div>
 <div id='modal-alert'></div>

 {{outlet modalBody}}

 {{#each errors}}
   <div class="alert alert-error">
     <button class="close" data-dismiss="alert">×</button>
     {{this}}
   </div>
 {{/each}}

在他们的 application.js.handlebars 中:{{render modal}}

于 2013-06-14T13:07:29.583 回答