1

Usually I find my self needing to write an object with a specific functionality that it is a set of models.

Finally I extend a collection and add more functions that works with its model.

I think is better show you an example:

My app has a set of permissions, related with the user and/or the version of the platform.

var Permissions = Backbone.Collection.extend({
   model: Permission,

   hasAccess: function (moduleCode) {
        ....
   },

   allowAccess: function (moduleCode) {
        ....
   },

   ...

With that methods I change the format of a property of a permission (the model). (My permissions are a concatenation of code with an string that identify the type of permission.)

A workmate tells me that it is wrong. In .NET he creates a class and he adds a private list and makes the changes to it. He does not inherit the list and changes it.

He would make a model and inside it he would add a collection property

this.set("permissionsCollection", new Backbone.Collection.extend({model: Permission}))

[Comment: I don't understand why he creates properties of everything, I think in his case it is not needed.] -> But this is another question

I think in a different way. I know the Collection has internally that list. I have a great potencial in Backbone.Collections, why do I have to use a model that it is not necessary? If I don't need that encapsulation... I think that it is not necessary, he is overprogramming in my opinnion.

Am I wrong? Did I not know how to use BackboneJS Collections?

Thank you in advance.

ccsakuweb
  • 789
  • 5
  • 17

1 Answers1

1

At the beginning I had something called helper with similar methods:

        findAttr: function (model, name) {
            var attrs = model.get('app_attrs');
            if (attrs !== undefined) {
                return this.findByAttrName(attrs, name);
            }
        },
        findByAttrName: function (array, name) {
            return _.find(array, function(a) {
                if (a.attrName === name) {
                    return a;
                }
            });
        }

The view code was more awkward:

        render: function () {
            var attr = helper.findAttr(this.model, 'user');
            ...
            return this;
        }

The only logical solution was to move these methods into the model (this.model in the above case). After refactoring I've got:

        render: function () {
            var attr = this.model.findAttr('user');
            ...
            return this;
        }

which is of course more readable than the previous solution.

Akos K
  • 7,071
  • 3
  • 33
  • 46
  • I see. I also do that all the time. My models are really custom. And when several models has common methods, but not all of them I create a super model that I will extend, or I create new properties (that contains other models, collection ..., outside defaults). I really like readibility too, is more easy to understand, so Its more easy to maintain. Then do you think is ok add more functionality to custom collections? – ccsakuweb Oct 02 '13 at 08:59
  • 1
    Sure, while those methods working with your collections' data (like in the example above) I think it's OK to group them this way. – Akos K Oct 02 '13 at 09:48