gmac / backbone.epoxy

Declarative data binding and computed models for Backbone
http://epoxyjs.org
MIT License
615 stars 89 forks source link

Accessing the parent view from within an itemView #84

Closed Prinzhorn closed 10 years ago

Prinzhorn commented 10 years ago

I came across multiple situations where I wished I could access the parent view (list) from within an itemView (list item).

Does this make sense? Maybe pass it to the itemView constructor as collectionView option.

maxpoulin64 commented 10 years ago

I had this problem too, I worked around it by overriding the itemView in my initialize function like this (if you didn't think of it already):

initialize: function() {
    // ...
    var self = this;
    this.itemView = function(options) {
        options.collectionView = self;
        return new MyChildVIew(options);
    }
}

I agree this should be passed by default, in the same way Backbone.Collection sets the collection attribute on its models. I also had the reverse problem (which I'll do my best to fix if I'm allowed to do what I said in #83)

Prinzhorn commented 10 years ago

Thanks, I didn't thought about that since I worked around it (I don't quite remember on what I was working six days ago o_O).

gmac commented 10 years ago

Sure, you've got it... added in commit 2eef8b4. An item view instance now receives a collectionView option when constructed. You may choose to persist that initialize option on the item view if you need it.

Prinzhorn commented 10 years ago

Awesome, thanks!

Prinzhorn commented 10 years ago

I fear you haven't tested it. Inside the each iterator function (https://github.com/gmac/backbone.epoxy/commit/2eef8b4921056d33735842d413f6087c481bc636#diff-56c53ff52b21a50cf38a0d0ffd2776c0R680) this refers to the window, making this.view undefined, resulting in collectionView being undefined as well.