2013-08-31 52 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中實現(當然,模態的處理不在插件中,但它呈現模板並觸發事件)。看看它,除此之外,它還有許多功能可以通過自動執行這些重複性任務來節省您的時間。