我应该如何在 Backbone.js 中构造类继承?
我正在为 Backbone 对象构建中间类:
例如,我有一个继承自 Backbone.Router
的 App.Router
,我的所有集合都将从 App 继承.Router
而不是 Backbone。
我不确定最佳实践是什么/或者它是否能正常工作。
我不太确定的一件事是如何结束构造函数,在 Backbone 的核心库中,它确实直接调用父级(在继承中),而我使用 __super__ 调用父级原型。
我还扩展了一个基本对象以启用通用方法。
这看起来可以吗?
App.Router = Backbone.Router.extend({
// Reference to views objects instanciated
views: {},
// Reference to collections objects instanciated
collections: {},
// Class constructor (can be overriden in subclass with the need of a parent call)
constructor: function(options) {
console.log(" \u2192App.Router::constructor()", [this, arguments]);
var self = this;
// Configure instance
this._configure(options || {});
// Extend App.Object
_.extend(this, App.Object);
// SO? : Is this the correct way to end constructor?
return App.Router.__super__.constructor.apply(this, arguments);
},
// Class initialization (override in subclass without the need of a parent call)
initialize: function(config) {
d&&console.log(this.name + "::initialize()", [this, arguments]);
},
// Performs the initial configuration of a Router with a set of options.
// Keys with special meaning are attached directly to the class
_configure : function(options) {
if (this.options) options = _.extend({}, this.options, options);
var classOptions = ['registerKey', 'parent', 'collections', 'views'];
for (var i = 0, l = classOptions.length; i < l; i++) {
var attr = classOptions[i];
if (options[attr]) this[attr] = options[attr];
}
this.options = options;
},
// Render a view with a collection & optional data
render: function(className, options) {
},
});
I'm building intermediate classes for Backbone objects :
For instance, i have a App.Router
that inherits from Backbone.Router
, all my collections will inherit from App.Router
instead of Backbone.
I'm not sure what the best-practice is / or if it will even work correctly.
One thing i'm not really sure is how to end the constructor, in Backbone's core lib, it does call directly the parent (in inherits), while i call the parent prototype with __super__
.
I also extend a base Object to enable generic methods.
Does this seems OK ?
App.Router = Backbone.Router.extend({
// Reference to views objects instanciated
views: {},
// Reference to collections objects instanciated
collections: {},
// Class constructor (can be overriden in subclass with the need of a parent call)
constructor: function(options) {
console.log(" \u2192App.Router::constructor()", [this, arguments]);
var self = this;
// Configure instance
this._configure(options || {});
// Extend App.Object
_.extend(this, App.Object);
// SO? : Is this the correct way to end constructor?
return App.Router.__super__.constructor.apply(this, arguments);
},
// Class initialization (override in subclass without the need of a parent call)
initialize: function(config) {
d&&console.log(this.name + "::initialize()", [this, arguments]);
},
// Performs the initial configuration of a Router with a set of options.
// Keys with special meaning are attached directly to the class
_configure : function(options) {
if (this.options) options = _.extend({}, this.options, options);
var classOptions = ['registerKey', 'parent', 'collections', 'views'];
for (var i = 0, l = classOptions.length; i < l; i++) {
var attr = classOptions[i];
if (options[attr]) this[attr] = options[attr];
}
this.options = options;
},
// Render a view with a collection & optional data
render: function(className, options) {
},
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会改变构造函数。您可以直接在初始化方法中完成所有这些操作。
所以:
在我看来,这会更干净。
I would not change the constructor. You can do all this directly in the initialize method.
So:
That will be much cleaner IMO.