0

I am looking for a way to have a BaseCollection for all my collections for shared behavior.

I was able to successfully do it with shared functions, but I want some initialization logic to occur in the BaseCollection so that all my collections trigger an event 'model-change'. Is there a way for my BaseCollection to have a constructor - is the code below the best way to do it? I assume if I put two initialize functions or two constructor override functions then only one will get called. So it seems I can only use one of them each.

  var BaseCollection = Backbone.Collection.extend({

          constructor: function () {

                var self = this;
                this.on('change',function(model,property){
                    self.trigger('model-change',model,property);
                });
              Backbone.Collection.apply(this, arguments);
            },

            parse: function (resp) {
                if (resp.success) {
                    return resp.success;
                }
                else if (resp.error) {
                    return this.models;
                }
                else {
                    return resp;
                }
            },

  });

  var UsersCollection = BaseCollection.extend({

            constructor: function () {   //this will override the constructor in BaseCollection, but I want both...
               this.givenName = '@UsersCollection';
               Backbone.Collection.apply(this, arguments);
            },

            initialize: function (models, opts) {
            //do something here 
            }

        });

Is there a better way to do it? One problem that I have is that I can't override constructor twice, I can only override it in the parent (BaseCollection) class. So is there a way to chain constructors? Or do I pretty much only have two options?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • You're still implementing `constructor` instead of `initialize`, why is that? You're also still doing `this.givenName = '@UsersCollection'` when `givenName: '@UsersCollection'` would make more sense. There are [lots of ways to implementing `super` in Backbone](http://stackoverflow.com/q/8596861/479863) so chaining shouldn't be a problem. – mu is too short Jul 24 '15 at 21:44
  • Mu, I was partly wrong. givenName: '@UsersCollection' definitely hides that property in the proto. However, setting this.givenName in the initialize function puts it in the top level of the Backbone object. So I will get rid of the constructor property in the subclass, but using the constructor property in the superclass seems to give me what I want. – Alexander Mills Jul 24 '15 at 22:04
  • without needing/wanting to use this.givenName in the constructor of the subclass, I have no current need to use the constructor property, so I will get rid of it at your suggestion – Alexander Mills Jul 24 '15 at 22:05

1 Answers1

0

You can probably just extend and apply from the BaseCollection instead of the Backbone.Collection to get the results you want from your example:

var BaseCollection = Backbone.Collection.extend({
    constructor: function () {
        // your base collection constructor

        // run the Backbone.Collection constructor on this
        Backbone.Collection.apply(this, arguments);
    }
});

// extends methods on BaseCollection
var UserCollection = BaseCollection.extend({
    constructor: function () {
        // your user collection constructor

        // run the BaseCollection constructor on this
        BaseCollection.apply(this, arguments);
    }
});
jungy
  • 2,932
  • 2
  • 18
  • 17