2011-12-18 8 views
1

次のビューとルーターのセットがあります。ページが最初に表示されたときに、「初期化」メソッドでナビゲートされた 'メソッドが呼び出されますが、要素は更新されません。別のビュー(hashbang)にナビゲートしてから戻ると、最初のビューが表示されます。ページが最初にレンダリングされたときに、バックボーン・ビューが表示されない

誰かが間違っていることを知っていますか?

var AppRouter = Backbone.Router.extend({ 

     contentView: null, 

     routes: { 
      'welcome': 'welcomeAction', 
      'settings': 'settingsAction', 
      'books': 'booksAction', 
      'book/:id': 'bookAction' 
     }, 

     initialize: function() { 
      this.navigate('welcome', true); 
     }, 

     welcomeAction: function() { 
      this.contentView = new views.WelcomeView({ 'model': new models.Welcome() }); 
     }, 

     settingsAction: function() { 
      this.contentView = new views.SettingsView({ 'model': new models.Settings() }); 
     }, 

     booksAction: function() { 
      this.contentView = new views.BooksView({ 'model': new models.Books() }); 
     }, 

     bookAction: function (id) { 
      this.contentView = new views.BookView({ 'model': new models.Book({ 'Id': id }) }); 
     } 
    }); 

    function App() { 

     this.router = null; 

     this.initialize = function() { 
      this.router = new AppRouter; 
      Backbone.history.start(); 
     }; 
    }; 

    var reader = new App; 
    reader.initialize(); 

ビューを以下に示します。

WelcomeView: Backbone.View.extend({ 

     'el': '#content-pane', 

     tagName: 'div', 

     template: _.template(templates.welcome), 

     events: { 
     }, 

     initialize: function() { 
      _.bindAll(this, 'render'); 
      this.renderLoading(); 
      this.model.fetch({ success: _.bind(function() { 
       this.model.set({ Date: new Date() }); 
       this.render(); 
      }, this) 
      }); 
     }, 

     renderLoading: function() { 
      var html = _.template(templates.loading)(); 
      $(this.el).empty(); 
      $(this.el).append(html); 
     }, 

     render: function() { 
      var html = this.template({ date: this.model.get('Date') }); 
      $(this.el).empty(); 
      $(this.el).append(html); 
     } 
    }), 
+0

なぜあなたは 'el'と' tagName'を同時に持っていますか? 'el'を定義するとき、既存の要素を参照する必要があり、' el'が定義されていない場合、 'tagName'、' className'および 'id'プロパティーが新しいものを作成するために使われます。 '#content-pane'が既に存在する場合は' tagName'を削除し、そうでない場合は 'tagName'と' id'を使用してください。 – kubetz

+0

'.empty()'の代わりに '.html(html)'を使用してください。 '.append(html)'を実行します。 – kubetz

+0

アドバイスありがとうございました。私はそれほど良く分かりませんでした。 – AwkwardCoder

答えて

1

は私に$(document).ready問題のように思えます。ビューを作成する前に、ページのすべてのコンテンツ(具体的には#content-pane)がロードされていますか?

+0

いいえわからない... – AwkwardCoder

+0

ビンゴ!あなたは正しいです :) – AwkwardCoder

関連する問題