Skip to content

Instantly share code, notes, and snippets.

@gavacho
Created November 12, 2012 21:06
Show Gist options
  • Save gavacho/4061898 to your computer and use it in GitHub Desktop.
Save gavacho/4061898 to your computer and use it in GitHub Desktop.
HauteLook.ChildViewObserver

This mixin, when applied to an Ember.ContainerView (and, by extension, a CollectionView) creates three new methods on the extended view. These methods are called whenever a contained view raises a willInsertElement, didInsertElement or willDestroyElement event.

    HauteLook.ChildViewObserver = Ember.Mixin.create({
        childViewsWillChange: function(childViews, start, removeCount, addCount) {
            this._super.apply(this, arguments);
            for(var i=0; i < removeCount; i++) {
                var cv = childViews.objectAt(start + i);
                cv.off('willInsertElement', this, this.willInsertChildElement);
                cv.off('didInsertElement', this, this.didInsertChildElement);
                cv.off('willDestroyElement', this, this.willDestroyChildElement);
            }
        },
        childViewsDidChange: function(childViews, start, removeCount, addCount) {
            this._super.apply(this, arguments);
            for(var i=0; i < addCount; i++) {
                var cv = childViews.objectAt(start + i);
                cv.on('willInsertElement', this, this.willInsertChildElement);
                cv.on('didInsertElement', this, this.didInsertChildElement);
                cv.on('willDestroyElement', this, this.willDestroyChildElement);
            }
        },
        willInsertChildElement: Ember.K,
        didInsertChildElement: Ember.K,
        willDestroyChildElement: Ember.K
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment