Backbone.js - 通过构造函数传递参数

发布于 2024-12-20 09:52:50 字数 793 浏览 0 评论 0原文

场景:
当我(尝试)通过构造函数设置 myVar 变量时,我收到一个 alert() 提示 undefined。但是,如果我取消注释 myView 内部的 myVar,警报就会显示“Hello from inside”,正如人们所期望的那样。

问题:
这是否意味着我无法在视图的构造函数中设置除骨干网自己的参数之外的任何参数,例如 modelcollectionel、<代码>id, className & 标签名称
手册:http://documentcloud.github.com/backbone/#View-constructor

代码:

var myView = Backbone.View.extend({

    //myVar : 'Hello from inside',

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-12-27 09:52:50

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

var myView = Backbone.View.extend({

  myVar : 'Hello from inside',

  initialize: function() {
      alert(this.options.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'});
海螺姑娘 2024-12-27 09:52:50

从主干 1.1.0 开始,options 参数不再自动附加视图(请参阅问题 2458 进行讨论)。您现在需要手动附加每个视图的选项:

MyView = Backbone.View.extend({
    initialize: function(options) {
        _.extend(this, _.pick(options, "myVar", ...));
        // options.myVar is now at this.myVar
    }
});

new MyView({
    myVar: "Hello from outside"
    ...
});

或者您可以使用这个迷你插件来自动-附加白名单选项,如下所示:

MyView = BaseView.extend({
    options : ["myVar", ...] // options.myVar will be copied to this.myVar on initialize
});

As of backbone 1.1.0, the options argument is no longer attached automatically to the view (see issue 2458 for discussion). You now need to attach the options of each view manually:

MyView = Backbone.View.extend({
    initialize: function(options) {
        _.extend(this, _.pick(options, "myVar", ...));
        // options.myVar is now at this.myVar
    }
});

new MyView({
    myVar: "Hello from outside"
    ...
});

Alternatively you can use this mini plugin to auto-attach white-listed options, like so:

MyView = BaseView.extend({
    options : ["myVar", ...] // options.myVar will be copied to this.myVar on initialize
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文