无法在同一个类中访问变量 [英] Can't access variable inside same class

查看:87
本文介绍了无法在同一个类中访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var BooksView = Backbone.View.extend({

    initialize: function() {
        this.render();
    },

    render: function() {
       alert(this.myVar);
    }
});

和我的路由器:

var BookRouter = Backbone.Router.extend({
   routes: {
      '': 'index',
      'list/:id': 'list'
   },

   index: function(){
      var booksView = new BooksView({
         el: ".content",
         myVar: "Example 1"
      });
   },
   list: function(id){
      var booksView = new BooksView({
         el: ".content",
         myVar: "Example 2"
      });
   }
});

如何访问变量?即使变量设置在同一类中,this.myVar似乎也不起作用?

How do I access the variable? this.myVar doesn't seem to be working, even though the variable is set inside the same class?

推荐答案

主链版本1.1.0 ribs.js不再自动为您附加所有选项,但是您仍然可以在初始化功能.

As of backbone version 1.1.0 backbone.js no longer attaches all options for you automatically, however you can still do this yourself manually inside your initialize function.

例如

 initialize: function(options) {
      if (options) {
        _.extend(this, options);
      }
      this.render();
    },

这篇关于无法在同一个类中访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆