2013-05-28 53 views
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.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.ecoBackbone.Marionette如何在新模型通過後將集合渲染到佈局中?

<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填充新模型的項目幻燈片?

回答

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