walmartlabs/thorax

Cannot access `this` inside itemView function in CollectionView

Opened this issue · 3 comments

Thorax.CollectionView.extend({
        collection: new NotesCollection(),
        itemView: function(item) {
            console.log(this);
            return new NoteView({
                model: item.model
            });
        }
    });

Why console.log(this) shows Thorax.CollectionView.extend.itemView {} instead of object? This is problematic for me because I need to access objects attributes inside this function. It only happens in itemView function.

This is certainly unexpected. Could you PR a failing test for this?

I became curious of this, and dove through the source.
Are you really meant to be able to use both factory-functions that return an itemView-instance and assign itemView-classes directly on the attribute?

Currently it seems like the code simply accepts the value as a viewclass if it is a function:
https://github.com/walmartlabs/thorax/blob/master/src/util.js#L249

Wouldn't it be hard to differentiate a factory-function from a Backbone class, since a Backbone class is a function itself?

If you could use a factory-function for itemView it would be useful for me as well :)
Then I would be in more control of what arguments are passed to the itemView-instance.

I can confirm that this is the case, and kind of a pain. I'll take a crack at a PR w/ test cases.