我应该如何在 Backbone.js 中构造类继承?

发布于 2024-12-19 14:16:59 字数 1874 浏览 1 评论 0原文

我正在为 Backbone 对象构建中间类:

例如,我有一个继承自 Backbone.RouterApp.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 技术交流群。

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

发布评论

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

评论(1

蹲在坟头点根烟 2024-12-26 14:16:59

我不会改变构造函数。您可以直接在初始化方法中完成所有这些操作。

所以:

App.Router = Backbone.Router.extend({
  initialize: function(options){
    this._configure(options)
    // no need to call super initialize as it is empty
  },
  _configure: function(){...}
});
_.extend(App.Router.prototype, App.Object);

在我看来,这会更干净。

I would not change the constructor. You can do all this directly in the initialize method.

So:

App.Router = Backbone.Router.extend({
  initialize: function(options){
    this._configure(options)
    // no need to call super initialize as it is empty
  },
  _configure: function(){...}
});
_.extend(App.Router.prototype, App.Object);

That will be much cleaner IMO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文