2012-08-17 130 views
2

我裝盤遷移我的骨幹木偶應用程序,但我在路由的錯誤:骨幹木偶路由

window.App = new Backbone.Marionette.Application 
    Models: {} 
    Collections: {} 
    Views: 
     Layouts: {} 
    Routers: {} 
    layouts: {} 
    Helpers: {} 

    init: ->   
     App.start() 
     App.main.show(App.layouts.main) 

App.addRegions 
    main: '#container' 

App.addInitializer (options) -> 
    new App.Routers.Profiles() 
    Backbone.history.start() 

$(document).ready -> 
    App.init() 

這是我的路由器

class App.Routers.Profiles extends Backbone.Marionette.AppRouter 
    routes: 
      'profiles/:id': 'show' 

    show: (id) -> 
      @profile = new App.Models.Profile(id: id) 
      view = new App.Views.ProfilesShow(model: @profile) 
      @profiles = new App.Collections.Profiles(@profile) 
      @profile.fetch() 
      App.layout.content.show(view) 

這是我的查看

class App.Views.ProfilesShow extends Backbone.Marionette.ItemView 
    template: JST['profiles/show'] 

    initialize: -> 
     @model.on('change', @render, @) 

    render: -> 
      $(@el).html(@template(profile: @model)) 
      @ 

這是我的主要佈局

class App.Views.Layouts.Main extends Backbone.Marionette.Layout 
    template: JST['layouts/main'] 

    regions: 
      content: '#content' 

App.addInitializer -> 
    App.layouts.main = new App.Views.Layouts.Main() 

當我試圖表明該行App.layout.content.show(視圖)的佈局來看,我有一個消息錯誤:「類型錯誤:應用程序。佈局未定義「。我不知道我是否在做一個好的練習。

回答

4

我找到了解決方案。我希望是更好的

主要的變化是在路由器中,我不得不添加一個控制器,所以新的路由器是

class App.Routers.Profiles extends Backbone.Marionette.AppRouter 
    appRoutes: 
    'profiles': 'index' 
    'profiles/new': 'new' 
    'profiles/:id': 'show' 

class App.Controllers.Profiles 
    constructor: -> 
     @collection = new App.Collections.Profiles() 
     @collection.fetch() 
     App.layouts.main = new App.Views.Layouts.Main() 

    show: (id) -> 
      profile = @collection.get(id) 
      view = new App.Views.ProfilesShow(model: profile) 
      App.main.show(App.layouts.main) 
      App.layouts.main.content.show(view) 

而且新的應用程序初始化爲:

window.App = new Backbone.Marionette.Application 
    Models: {} 
    Collections: {} 
    Views: 
      Layouts: {} 
    Routers: {} 
    layouts: {} 
    Helpers: {} 
    Controllers: {} 

    init: -> 
      App.start() 

App.addInitializer (options) -> 
    App.addRegions(main: '#container') 
    controller = new App.Controllers.Profiles 
    new App.Routers.Profiles(controller: controller) 
    Backbone.history.start() 

$(document).ready -> 
    App.init()