简体   繁体   English

如何将属性传递给Backbone视图?

[英]How do I pass properties to a Backbone view?

I was developing my app using Backbone v1.0.0 and between beginning work and now there has been an update to v1.1.0. 我正在使用Backbone v1.0.0开发应用程序,从开始工作到现在,已经对v1.1.0进行了更新。 So where I used to be able to do, 所以我曾经能够做到的,

var myView = new MyView({hash: 'something'});

And access hash inside the view using, 并使用,访问视图内的hash

this.options.hash

This no longer works. 这不再有效。 I now get the following error: 我现在收到以下错误:

Uncaught TypeError: Cannot read property 'hash' of undefined 未捕获的TypeError:无法读取未定义的属性“hash”

So what's the new way of doing this? 这样做的新方法是什么? I would very much like to be able to pass properties into my view. 我非常希望能够将属性传递到我的视图中。

From the 1.1.0 ChangeLog : 1.1.0 ChangeLog

  • Backbone Views no longer automatically attach options passed to the constructor as this.options , but you can do it yourself if you prefer. 骨干视图不再自动将传递给构造函数的选项附加为this.options ,但是您可以根据需要自己进行操作。

So the constructor options are still passed to initialize but this.options is no longer automatically set up. 因此,仍会传递构造函数选项以进行initialize但是this.options不再自动设置。 You can do this: 你可以这样做:

initialize: function(options) {
    // Stash `options.hash` in `this` if you want or
    // `this.options = options;` if you want to emulate
    // the old behavior.
}

Demo: http://jsfiddle.net/ambiguous/SaJkz/ 演示: http//jsfiddle.net/ambiguous/SaJkz/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM