Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created June 25, 2012 18:56
Show Gist options
  • Save jshirley/2990516 to your computer and use it in GitHub Desktop.
Save jshirley/2990516 to your computer and use it in GitHub Desktop.
views : {
GoalListView : {
type : NS.GoalListView,
requires : [ 'Person', 'CategoryList', 'GoalList', 'NoteList' ],
preserve : true
}
},
showGoalView : function(req, res, next) {
this.handleView('GoalListView', req, res, next);
}
/**
Setup all required models that are not loaded yet, and load them.
Once complete, show the requested view.
@method handleView
@param view {String} Name of the view to show
@param req {Request} The request
@param next {Function} The function to execute after showing the view
**/
handleView: function(view, req, next, cb) {
var viewObj = this.views[view],
requires = viewObj.requires,
views = viewObj.views,
config = Y.merge( ( viewObj.config || {} ), req.viewConfig ),
self = this,
stack = new Y.Parallel();
if ( !cb ) { cb = function() { }; }
if ( !config ) {
config = req.viewConfig = {};
}
if ( Y.Lang.isArray( requires ) ) {
Y.Array.each( requires, function(requirement) {
if ( ! self.models[ requirement ] ) {
if ( ! NS[requirement] ) {
throw "Invalid model `" + requirement + "` asked for. Check requirements for the view " + view;
}
var mc = ( req.modelConfig || {} ),
cfg = ( mc[requirement] || {} ),
m = self.models[requirement] = new NS[requirement](cfg);
// The config to call in showView should have this added in
if ( ! config[ requirement ] ) {
config[ requirement ] = m;
}
// Don't emit any events, since we want to setup.
m.load({ silent : true }, stack.add() );
} else {
config[requirement] = self.models[requirement];
}
});
} else {
// Just a noop, expand this if we want to do something without
// requires.
//stack.add( function() { Y.log('briiiiiing'); return 1; } );
}
Y.log('handleView complete, waiting for events to end.', 'debug', this.name);
stack.done( function() {
Y.log('stack.done complete, showing view: ' + view , 'debug', this.name);
Y.Array.each( views, function(subView) {
config[subView] = self.getViewInfo(subView);
});
self.showView(view, config, cb);
next();
});
return viewObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment