Last active
August 24, 2017 17:06
-
-
Save paulfalgout/00738a248920dd04a4fc6fa52b755723 to your computer and use it in GitHub Desktop.
Make the Marionette.NextCollectionView act like a CompositeView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To use: | |
* Mn.NextCollectionView.extend({ | |
* initialize() { | |
* new MakeComposite({ el: this.el, collectionView: this }); | |
* } | |
* // Regular CompositeView API | |
* }); | |
* | |
*/ | |
import _ from 'underscore'; | |
import Marionette from 'backbone.marionette'; | |
const CompositeOptions = [ | |
'childViewContainer', | |
'collection', | |
'model', | |
'template', | |
'templateContext' | |
]; | |
const MakeCompositeView = Marionette.View.extend({ | |
initialize(options) { | |
this.mergeOptions(options, ['collectionView']); | |
_.extend(this.options, this.collectionView.options); | |
_.each(CompositeOptions, opt => { | |
this[opt] = this.collectionView.getOption(opt); | |
}); | |
this.listenTo(this.collectionView, { | |
'before:render': this.onBeforeRenderCollectionView, | |
'before:destroy': this.onBeforeDestroyCollectionView | |
}); | |
this.collectionView.attachHtml = _.bind(this._attachHtml, this); | |
if(!this.collectionView.childView) { | |
this.collectionView.childView = this.collectionView; | |
} | |
}, | |
_attachHtml(els) { | |
this.Dom.appendContents(this.$container[0], els, {_$el: this.$container}); | |
}, | |
onBeforeRenderCollectionView() { | |
this.render(); | |
this.collectionView.bindUIElements(); | |
this.getChildViewContainer(); | |
}, | |
getChildViewContainer() { | |
const childViewContainer = _.result(this.collectionView, 'childViewContainer'); | |
this.$container = childViewContainer ? this.$(childViewContainer) : this.$el; | |
}, | |
onBeforeDestroyCollectionView() { | |
this.destroy(); | |
} | |
}); | |
export default function(collectionView) { | |
new MakeCompositeView({ el: collectionView.el, collectionView }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment