gpt4 book ai didi

marionette - Backbone Marionette 路由

转载 作者:行者123 更新时间:2023-12-04 18:14:22 25 4
gpt4 key购买 nike

我正在准备使用 Backbone Marionette 迁移我的应用程序,但路由中出现错误:

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(view) 行中的布局中显示 View 时,出现消息错误:“TypeError: App.layout is undefined”。我不知道我是否做得很好。

最佳答案

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

主要的变化在路由器,我不得不添加一个 Controller ,所以新的路由器是

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)

新的 App Init 是:
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()

关于marionette - Backbone Marionette 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11999466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com