2016-11-05 127 views
1

我的路由器訪問變量

var AppRouter = Backbone.Router.extend({ 
     routes: { 
      ':*':'home', 
      'home' : 'home', 
      'home/:a' : 'home', 
      '*whatever' : '404' 

     }, 
     home: function (a) { 
      document.title = siteTitle + ' - Home'; 
      homePage = new HomePage; 
      homePage.render(a); 
     }, 
     404: function(){ 
      document.title = siteTitle + ' - 404'; 
      fofPage = new FoFPage; 
      fofPage.render(); 
     } 

    }); 

我家的觀點

var HomePage = Backbone.View.extend({ 
     initialize: function (options) { 
      _.bindAll(this, 'beforeRender', 'render', 'afterRender'); 
      var _this = this; 
      this.render = _.wrap(this.render, function (render) { 
       _this.beforeRender(); 
       render(); 
       _this.afterRender(); 
       return _this; 
      }); 
     }, 

     beforeRender: function() { 
      console.log('beforeRender'); 
     }, 

     el: $('#indexcontent'), 

     template: _.template(homeHTML, {}), 

     render: function (a) { 
      (a == null) ? Backbone.history.navigate('home'): Backbone.history.navigate('home/' + a); 
      console.log('Passed parameter is ' + a); 
      this.$el.html(this.template); 

     }, 

     afterRender: function() { 
      $('pre code').each(function (i, block) { 
       hljs.highlightBlock(block); 
      }); 
      console.log('afterRender'); 
     } 
    }); 

我想抓住從路由器變量a查看。但它顯示在我的控制檯中未定義。我在初始化時嘗試了變量options,但沒有運氣。 感謝

+0

PS:https://bitbucket.org/sizil/backbone-requireboiler/overview這是Backbone JS和RequireJS的示例boilet板,如果有人想的話。這是我的第一次回購,所以請原諒我的罪過。 – unkn0wn

+0

Hardcoding'el:$('#indexcontent')'是個壞主意。該選項應該是動態的。你可以用'id'來簡單地裝飾默認元素 –

+0

我同意硬編碼元素是一個壞主意。但是對於這個特殊的骨架,我需要硬編碼,因爲我的索引頁面在加載器中是空白的。 – unkn0wn

回答

1
var AppRouter = Backbone.Router.extend({ 
     routes: { 
      ':*':'home', 
      'home' : 'home', 
      'home/:a' : 'home', 
      '*whatever' : '404' 

     }, 
     home: function (a) { 
      document.title = siteTitle + ' - Home'; 
      homePage = new HomePage({route: a}); 
      homePage.render(); 
     }, 
     404: function(){ 
      document.title = siteTitle + ' - 404'; 
      fofPage = new FoFPage; 
      fofPage.render(); 
     } 

    }); 

查看

var HomePage = Backbone.View.extend({ 
     initialize: function (options) { 
      this.options = options; 
      _.bindAll(this, 'beforeRender', 'render', 'afterRender'); 
      var _this = this; 
      this.render = _.wrap(this.render, function (render) { 
       _this.beforeRender(); 
       render(_this.options['route']); 
       _this.afterRender(); 
       return _this; 
      }); 
     }, 

     beforeRender: function() { 
      console.log('beforeRender'); 
     }, 

     el: $('#indexcontent'), 

     template: _.template(homeHTML, {}), 

     render: function (a) { 
      (a == null) ? Backbone.history.navigate('home'): Backbone.history.navigate('home/' + a); 
      console.log('Passed parameter is ' + a); 
      this.$el.html(this.template); 

     }, 

     afterRender: function() { 
      $('pre code').each(function (i, block) { 
       hljs.highlightBlock(block); 
      }); 
      console.log('afterRender'); 
     } 
    }); 
2

當你找到了答案,我將解釋爲什麼您最初的代碼不能正常工作,甚至更多。

爲什麼要傳遞給render不起作用?

因爲您使用underscore's wrap函數覆蓋initialize函數中的render函數並使用包裝版本。

它可以工作,但包裝時你需要採取的參數考慮:

this.render = _.wrap(this.render, function(render, a) { 
    this.beforeRender(); 
    render.call(this, a); // here, pass the argument 
    this.afterRender(); 
    return this; 
}); 

還要注意,你不需要var _this = this因爲你只是更換成員功能上下文在被調用時自動應用。 _.bindAll這是沒用的。

更簡單的方法

這包東西是不必要的,它只是complexifying的HomePage觀點沒有任何好處或添加的功能。

的唯一的事情你需要:

var HomePage = Backbone.View.extend({ 
    el: $('#indexcontent'), 

可以忽略選項參數(該{}

template: _.template(homeHTML), 

    initialize: function(options) { 

,如果你想通過選項進行初始化,使用以下模式之一:

 this.options = options || {}; // make sure it's an object. 

或者更好的是,確保它是一個拷貝的對象,具有默認值。

 this.options = _.extend({ 
      route: "default-value" 
     }, options); 
    }, 

    beforeRender: function() { 
     console.log('beforeRender'); 
    }, 

    render: function(route) { 
     this.beforeRender(); 

在路由器中做重定向,這是他的角色。

 // (a == null) ? Backbone.history.navigate('home'): Backbone.history.navigate('home/' + a); 

     console.log('Passed parameter is ' + route || this.options.route); 

underscore's _.template函數返回一個函數,所以你需要調用它。

 this.$el.html(this.template()); 

     this.afterRender(); 
     return this; 
    }, 

    afterRender: function() { 

避免全球jQuery的功能和範圍界定喜歡你搜索到的視圖元素及其孩子this.$(selector)

 this.$('pre code').each(function(i, block) { 
      hljs.highlightBlock(block); 
     }); 
     console.log('afterRender'); 
    } 
}); 
+1

嘿Emile!我剛剛注意到我最近見過你! :D和耶謝謝你幫助我,我開始寫這個新的僞代碼感謝你在我最後一個問題(Javascript SQL)中的答案。失眠之夜夫婦,我知道骨幹。 – unkn0wn

+0

雖然我想嘗試我的手Angular和一些令人印象深刻的圖形設計。回頭見。 – unkn0wn

+0

@ unkn0wn是啊,我主要是圍繞[標籤:backbone.js]問題 –