Backbone.js的 - 传递参数的构造函数,通过 [英] Backbone.js - passing arguments through constructors

查看:257
本文介绍了Backbone.js的 - 传递参数的构造函数,通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情景:结果
我得到了一个警报()未定义当我(尝试)设置 myVar的通过构造函数的变量。但是如果我取消注释 myVar的是这样的MyView的生活里面,该警报则不用说你好从内,正如人们所期望的。

问:结果
这是否意味着我不能设置任何PARAMS在视图中,除了骨干网构造自己的参数,可以如模式 ID 的className &安培; 标签名?结果
手动:<一href=\"http://documentcloud.github.com/backbone/#View-constructor\">http://documentcloud.github.com/backbone/#View-constructor

的code:

  VAR MyView的= Backbone.View.extend({    // myVar的:你好从内',    初始化:功能(){
        警报(this.myVar);
    }
)};新的MyView的({myVar的:你好从外面'});


解决方案

选项传递到构造函数会被自动存储为 this.options

  VAR MyView的= Backbone.View.extend({  myVar的:你好从内',  初始化:功能(){
      警报(this.options.myVar);
  }
)};新的MyView的({myVar的:你好从外面'});

Scenario:
I got an alert() saying undefined when I (try to) set the myVar variable through the Constructor. However if I uncomment the myVar that's living inside the myView, the alert then instead says "Hello from inside", just as one would expect.

Question:
Does this mean that I cannot set any params in the constructor of the view except backbones own params, such as model, collection, el, id, className & tagName?
Manual: http://documentcloud.github.com/backbone/#View-constructor

The code:

var myView = Backbone.View.extend({

    //myVar : 'Hello from inside',

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

new myView({myVar: 'Hello from outside'});

解决方案

Options passed into the constructor are automatically stored as this.options

var myView = Backbone.View.extend({

  myVar : 'Hello from inside',

  initialize: function() {
      alert(this.options.myVar);
  }
)};

new myView({myVar: 'Hello from outside'});

这篇关于Backbone.js的 - 传递参数的构造函数,通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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