Backbone.Marionette新模型传递后如何将集合渲染到布局中?

时间:2013-05-28 17:05:54

标签: backbone.js marionette

查看代码

@Portfolio.module "ProjectsApp.Show", (Show, App, Backbone, Marionette, $, _) ->

  class Show.ProjectsLayout extends Backbone.Marionette.Layout
    template: JST["backbone/apps/projects/templates/list_layout"]

    regions: 
     projectSlidesRegion  : "#project-slides"
     projectDetailsRegion : "#project-details"
     projectListRegion    : "#project-list"

    initialize: ->
      console.log "ProjectsLayout initialized"

  class Show.Detail extends Backbone.Marionette.ItemView
    template: JST["backbone/apps/projects/templates/_project_detail"]    

    modelEvents:
      "change" : "render"
      "add"    : "render"

    initialize: ->
      console.log "Show.Detail initialized"
      console.log this.collection


    #Project Slides


   class Show.ProjectSlide extends Backbone.Marionette.ItemView
     template: JST["backbone/apps/projects/templates/_project_slide"]
     tagName: "li"

     initialize: ->
       console.log "ProjectSlide View initialized"
       console.log this.model.get('avatar')

   class Show.ProjectSlides extends Backbone.Marionette.CompositeView #CollectionView
     template: JST["backbone/apps/projects/templates/project_slides"]
     itemView: Show.ProjectSlide
     itemViewContainer: "ul"
     initialize: ->
       console.log "ProjectSlides View initialized"

     collectionEvents:
      "change": "render"
      "add"   : "render"


#   Project Name List Code

  class Show.ProjectName extends Backbone.Marionette.ItemView
    template: JST["backbone/apps/projects/templates/_project_name_on_project_list"]
    tagName: "li"

     events:
      "click a.project-link" : -> 
        Portfolio.AboutApp.Show.trigger "project-name:link:clicked", @model

    triggers:
      "click .project-link" : "project:link:clicked" 


  class Show.ProjectList extends Backbone.Marionette.CompositeView
    template: JST["backbone/apps/projects/templates/project_list"]
    tagName: "ul"

    triggers:
      "click .project-link" : "project:link:clicked" 

控制器

@Portfolio.module "ProjectsApp.Show", (Show, App, Backbone, Marionette, $, _) ->

  Show.Controller =

    showProjects: ->
      projects = Portfolio.projects
      project = projects.at(0)
      @projectsLayout = @getProjectLayoutView()

      @projectsLayout.on "show", =>
        @showDetails project
        @showProjectSlides project
        @showProjectList projects

      App.mainRegion.show @projectsLayout

    showDetails: (project) ->
      projectDetailsView = @getProjectDetailsView project
      @projectsLayout.projectDetailsRegion.show projectDetailsView

    showProjectSlides: (project) ->
      console.log "activated showProjectSlides"
      projectSlides = project.get('project_slides')
      projectSlidesCollection = new Portfolio.Entities.SlidesCollection projectSlides
      slidesView = @getSlidesView projectSlidesCollection
      @projectsLayout.projectSlidesRegion.show slidesView      

    showProjectList: (projects) ->
      projectListView = @getProjectListView projects
      Portfolio.AboutApp.Show.on "project-name:link:clicked", (project) =>
        @showProject project

      @projectsLayout.projectListRegion.show projectListView

       #   Show Specific Project 

    showProject: (project) ->
      console.log project
#      @showProjectSlides project
      projectSlides = project.get('project_slides')
      projectSlidesCollection = new Portfolio.Entities.SlidesCollection projectSlides
#      console.log "showProject projectSlides: #{projectSlidesCollection}"
      projectSlides = projectSlidesCollection.reset projectSlides
      newProjectView = @getProjectDetailsView project
      newProjectSlidesView = @getSlidesView projectSlides
      @projectsLayout.projectDetailsRegion.show newProjectView
      @projectsLayout.projectSlidesRegion.show newProjectSlidesView

   # If I comment out the lines  newProjectView = @getProjectDetailsView project and  @projectsLayout.projectDetailsRegion.show newProjectView the projectSlidesRegion populates in the view with no issues, but no projectDetailsRegion updates

     getProjectListView: (projects) ->
       new Show.ProjectList
         collection: projects

     getSlidesView: (projectSlides) ->
#       console.log "projectSlides: #{projectSlides}"
       new Show.ProjectSlides
         collection: projectSlides


    getProjectLayoutView: -> 
      new Show.ProjectsLayout 

   # not sure if I need to explicitly pass the children objects to the view?
    getProjectDetailsView: (project) ->
      projectSlides = project.get('project_slides')
      new Show.Detail
        model: project
        collection: new Portfolio.Entities.SlidesCollection projectSlides

模板:

 list_layout.jst.eco:
    <div id="project-details"></div>
    <div id="project-list"></div>

  _project_detail.jst.eco:

    <img class="project-icon" src='assets/<%= @logo %>'>
    <h3><%= @name %></h3>

    <div id="tldr-wrapper">
      <article id="project-problem">
       <img src = "#" alt="problem pic">
       <h3 class="project-header">problem</h3>
        <p class="project-paragraph"><%= @problem %></p>
      </article>

      <article id="project-solution">
       <img src = "#" alt="solution pic">    
        <h3 class="project-header">solution</h3>
        <p class="project-paragraph"><%= @solution %></p>
     </article>
    </div>

    <!-- how do I get this to show when I click on the new project from the project's list   in the projectListRegion? -->
      <div id="project-slides"></div>
    <!-- end -->

    <p><%= @detail %></p>

_project_slide.jst.eco

<img src='assets/<%= @avatar %>' />
<%= @caption %>

json对象:

{
 "id":1,
 "name":"project name",
 "logo":"project-icon.png",
 "problem":"The project's problem.",
 "detail":"project's details",
 "project_slides":
    [
     {
      "id":1,
      "project_id":1,
      "avatar":"blue-disc.png",
      "caption":"what a lovely disc"
      },         
     {
      "id":3,
      "project_id":1,
      "avatar":"desktop.png",
      "caption":"yay computer"
     }
    ]
 }

当我第一次打开页面时,项目和项目的详细信息以及项目列表会正确显示。当我从项目列表中单击项目名称时,我希望使用新项目的属性和项目列表更新projectDetailsRegion和projectSlidesRegion以保持不变。项目列表正常显示所有项目名称。但是只更新projectDetailsRegion并且projectSlides为空。如何让projectSlidesRegion填充新模型的项目幻灯片?

1 个答案:

答案 0 :(得分:0)

查看

@Portfolio.module "ProjectsApp.Show", (Show, App, Backbone, Marionette, $, _) ->

  class Show.ProjectsLayout extends Backbone.Marionette.Layout
    template: JST["backbone/apps/projects/templates/list_layout"]

    regions: 
     projectSlidesRegion  : "#project-slides"
     projectDetailsRegion : "#project-details"
     projectListRegion    : "#project-list"

    initialize: ->
      console.log "ProjectsLayout initialized"

  class Show.Detail extends Backbone.Marionette.Layout # <-- changed this to a layout 

    template: JST["backbone/apps/projects/templates/_project_detail"]    

    regions: 
     projectSlidesRegion  : "#project-slides"


    itemView: Show.ProjectSlides
    itemViewContainer: "#project-slides"
    modelEvents:
      "change" : "render"
      "add"    : "render"

    initialize: ->
      console.log "Show.Detail initialized"
      console.log this.collection


#       Project Slides


   class Show.ProjectSlide extends Backbone.Marionette.ItemView
     template: JST["backbone/apps/projects/templates/_project_slide"]
     itemViewContainer: "#project-slides"
     tagName: "li"

     initialize: ->
       console.log "ProjectSlide View initialized"
       console.log this.model.get('avatar')

   class Show.ProjectSlides extends Backbone.Marionette.CompositeView #CollectionView
     template: JST["backbone/apps/projects/templates/project_slides"]
     itemView: Show.ProjectSlide
     itemViewContainer: "ul"
     initialize: ->
       console.log "ProjectSlides View initialized"

     collectionEvents:
      "change": "render"
      "add"   : "render"


#       Project Name List Code

  class Show.ProjectName extends Backbone.Marionette.ItemView
    template: JST["backbone/apps/projects/templates/_project_name_on_project_list"]
    tagName: "li"

    events:
      "click a.project-link" : -> 
        Portfolio.AboutApp.Show.trigger "project-name:link:clicked", @model

    triggers:
      "click .project-link" : "project:link:clicked" 


  class Show.ProjectList extends Backbone.Marionette.CompositeView
    template: JST["backbone/apps/projects/templates/project_list"]
    itemView: Show.ProjectName
    tagName: "ul"

    triggers:
      "click .project-link" : "project:link:clicked" 

控制器

        # Show Specific Project 


showProject: (project) ->
  console.log project
  projectSlides = project.get('project_slides')
  projectSlidesCollection = new Portfolio.Entities.SlidesCollection projectSlides
  projectSlides = projectSlidesCollection.reset projectSlides
  newProjectView = @getProjectDetailsView project
  newProjectSlidesView = @getSlidesView projectSlides
  showDetailView = @getProjectDetailsView project
  @projectsLayout.projectDetailsRegion.show showDetailView
  showDetailView.projectSlidesRegion.show newProjectSlidesView # <-- All set; just needed a layout nested within a layout