Created
November 23, 2011 13:39
-
-
Save jakobdamjensen/1388681 to your computer and use it in GitHub Desktop.
Containerview, mixin and controller
This file contains 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
-- 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