2013-10-27 28 views
0

如果我在我的骨幹應用的列表視圖中單擊鏈接,則單個視圖將按其應有的方式呈現。在瀏覽器歷史中來回導航也適用。但是,如果我在瀏覽器中重新加載頁面(單個視圖)或手動輸入該特定單個視圖的URL,則不起作用。然後該集合不包含任何模型。在這種情況下,如何獲得呈現單個視圖所需的特定模型?謝謝在頁面重新加載後骨幹單視圖無法工作

您main.coffee之前您的收藏已完成取

class window.MovieSingleView extends Backbone.View 
    tagName: 'div' 
    className: 'row' 

    initialize: -> 
     @template = _.template ($ '#movie-single-view-template').html() 

    render: -> 
     that = @ 

     @model.deferred.done -> 
     ($ that.el).html(that.template that.model.toJSON()) 

     @ 

    class window.Router extends Backbone.Router 
    routes: 
     '': 'index' 
     'movies/:id': 'movieSingleView' 

    initialize: -> 
     @collection = new Movies() 
     @collection.fetch 
     reset: true 

    index: -> 
     moviesView = new MoviesView 
     # collection: window.movies 
     collection: @collection 

     ($ '#container') 
     .empty() 
     .append moviesView.render().el 

    movieSingleView: (id) -> 
     # movie = window.movies.get id 
     # decoratedMovie = new DecoratedMovie movie 

     movie = @collection.get id 
     decoratedMovie = new DecoratedMovie movie 

     movieSingleView = new MovieSingleView 
     model: decoratedMovie 

     ($ '#container') 
     .empty() 
     .append movieSingleView.render().el 

    $ -> 
    window.app = new Router() 

    Backbone.history.start 
     pushstate: true 

    # window.movies.fetch 
    # reset: true 

的index.html

<script type="text/template" id="movie-view-template"> 
    <td><%= _year %></td> 
    <td><a href="#/movies/<%= _id %>"><%= _title %></a></td> 
    <td><%= _watched %><td> 
    <td><span class="glyphicon glyphicon-remove pull-right remove"></span></td> 
</script> 

回答

1

你的單一視圖渲染。所以,你只需要在收集完成後渲染視圖!