我在开始工作期间使用 Backbone v1.0.0 开发我的应用程序,现在已经更新到 v1.1.0。所以我以前能做到的地方,
var myView = new MyView({hash: 'something'});
并在 View 中访问 hash
,
this.options.hash
这不再有效。我现在收到以下错误:
Uncaught TypeError: Cannot read property 'hash' of undefined
那么这样做的新方法是什么?我非常希望能够将属性传递到我的 View 中。
最佳答案
来自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.
因此构造函数选项仍然传递给 initialize
但 this.options
不再自动设置。你可以这样做:
initialize: function(options) {
// Stash `options.hash` in `this` if you want or
// `this.options = options;` if you want to emulate
// the old behavior.
}
关于javascript - 如何将属性传递给 Backbone View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19370975/