访问变量

问题描述:

我的路由器访问变量

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

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'); 
     } 
    }); 

当你找到了答案,我将解释为什么您最初的代码不能正常工作,甚至更多。

为什么要传递给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]问题 –