Skip to content

Instantly share code, notes, and snippets.

@jakobdamjensen
Created November 23, 2011 13:39
Show Gist options
  • Save jakobdamjensen/1388681 to your computer and use it in GitHub Desktop.
Save jakobdamjensen/1388681 to your computer and use it in GitHub Desktop.
Containerview, mixin and controller
-- ContainerView
Mango.MainContentContainerView = SC.ContainerView.extend(Mango.ContainerViewSwapperMixin, {
activeViewBinding: "Mango.mainContentViewController.activeView"
});
--- MIXIN
Mango.ContainerViewSwapperMixin = {
_cachedView: [],
_activeViewDidChange: function () {
var activeView = this.get('activeView'),
childViews = this.get('childViews'),
instantiatedView,
viewClass;
this.removeAllChildren();
if(activeView) {
if(activeView.isClass) {
viewClass = activeView;
} else if(typeof activeView === 'string') {
viewClass = SC.getPath(activeView);
}
if (viewClass && viewClass.isClass) {
var view = undefined;
this.get("_cachedView").forEach(function(item){
if(item.classT === viewClass){
view = item.view;
return;
}
});
if(view){
console.log("use cached");
childViews.pushObject(view);
} else {
console.log("make new");
view = viewClass.create();
this.get("_cachedView").pushObject({
view: view,
classT: viewClass
});
childViews.pushObject(view);
}
} else {
console.error("Unknown view: " + activeView);
}
}
}.observes('activeView')
};
--- content view controller
Mango.mainContentViewController = SC.Object.create({
activeView: "",
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment