paniclater
paniclater

Reputation: 913

Avoid Javascript "The local variable may not have been initialized" warning in backbone.js Collection

Eclipse says that EventCategories(active) is a potential local variable that hasn't been initialized. I don't see how this how to avoid this. Any help? I'm also a Backbone.js newb, so I'm not sure that I'm actually doing this right (even though it works).

define([ "modules/config/model/event-category" ], function(EventCategory) {
    var EventCategories = Backbone.Collection.extend({
        model : EventCategory,
        url : restEndpoint + "/config/category",
        getActive : function() {
            var active = this.filter(function(eventCategory){
                return eventCategory.get("active") === true;
            });
            return new EventCategories(active);
        }
    });
    return EventCategories;
});

Upvotes: 0

Views: 756

Answers (1)

mu is too short
mu is too short

Reputation: 434665

I suppose you could use this.constructor as a backdoor way to getting at EventCategories:

Object.prototype.constructor

Returns a reference to the Object function that created the instance's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name.

So if this is an EventCategories instance then this.constructor === EventCategories. In your case, you could say:

return new this.constructor(active);

Upvotes: 1

Related Questions