2013-08-31 40 views
2

我想代替在#content中呈现模板,首先动态显示模态对话框,然后模板。应该在哪里负责显示自动包含所选模板的模态?毕竟,如何取消后关闭并删除模态类?这是当前代码:git。我正在学习骨干,不知道什么是正确的模式。如何在模态模板对话框中显示嵌入的模板?

会话路由器:

($, Backbone, App, Session, Layout, AlertQueue) -> 
    class App.Routers.SessionsRouter extends Backbone.Router 
    initialize: (options) -> 
     @user = options.user 
     @token = options.token 
     @session = new Session(@user) 
     @user.on('change', => 
     @session = new Session(@user) 
     @token.fetch() 
    ) 

    routes: 
     "signup":    "newRegistration" 
     "signin":    "newSession" 

    newRegistration: -> 
     View = require 'views/registrations/new_view' 
     Model = require 'models/registration' 
     @view = new View(model: new Model(), user: @user) 
     # Layout.setContent(@view) 
     # Dialog.show(@view)?? 

    newSession: -> 
     View = require 'views/sessions/new_view' 
     @view = new View(model: @session, user: @user, token: @token) 
     # Layout.setContent(@view) 

布局帮手,现在显示静态容器内容:

($, Backbone, App) -> 
    class Layout extends Backbone.Model 
    setContent: (content) -> 
     do @currentContent.close if @currentContent? 
     @currentContent = content 
     ($ '#content').html content.render().el 

    App.Helpers.Layout = new Layout 

当前模式的模板:

#dialog.modal.hide.fade 
    .modal-header 
    %a.close{href: "#"} × 
    /%h3=title 
    .modal-body 
    #dialog_content 
    .modal-footer 
    %a.btn.danger.yes Yes 
    %a.btn.secondary.no No 

当前模态对话框观点:

($, Backbone, App) -> 

    class App.Views.Common.DialogView extends Backbone.View 
    template: JST["common/dialog"] 

    initialize: (options) -> 
     super(options) 

    render: -> 
     $(@el).html(@template()) 
     $('.modal', @el).modal() 
     return @ 

    show: -> 
     $('.modal', @el).modal('show') 

    hide: -> 
     $('.modal', @el).modal('hide') 

当前对话框初始化:

($, Backbone, App, FormView, DialogModalView) -> 

    class App.Views.Common.DialogView extends FormView 

    className: -> "modal" 

    initialize: -> 
     view = new DialogModalView() 

     $(view.render().el).find(".modal-body").append(@template()) 
     $(view.render().el.children).modal('show') 

会话视图扩展对话视图:

($, Backbone, App, Session, DialogView) -> 

    class App.Views.Sessions.NewView extends DialogView 
    template: JST["sessions/new"] 
+0

因此,如果我理解正确,你不想在你的视图中调用'modal('show')',而是想让Bootstrap为你做到这一点,所以你可以呈现任何内部模式? – j03w

回答

1

一个更好的方法是将相关的逻辑的观点,这使得模板的模式。 当视图的渲染方法调用它渲染模板,然后将具体视图逻辑与常见模态功能分离时,它应该触发一个事件,例如渲染。

视图本身可以侦听此事件,以及何时触发渲染模板上的调用模式。您可以将回调命名为onRender。如果模态与某些应用程序逻辑相关,则可以使用此事件驱动方法在模型外部处理模态创建。

这个事件驱动的模板和回调逻辑在Backbone Marionette中实现(当然,模态的处理不在插件中,但它呈现模板并触发事件)。看看它,除此之外,它还有许多功能可以通过自动执行这些重复性任务来节省您的时间。