Skip to content

Instantly share code, notes, and snippets.

@ericf
Created July 13, 2012 17:58
Show Gist options
  • Save ericf/3106306 to your computer and use it in GitHub Desktop.
Save ericf/3106306 to your computer and use it in GitHub Desktop.
/**
Provides Pjax-style content fetching and handling for `Y.App`.
@module app
@submodule app-content
@since 3.6.0
**/
var Lang = Y.Lang,
PjaxContent = Y.PjaxContent;
/**
Provides Pjax-style content fetching and handling for `Y.App`.
@class App.Content
@uses Pjax.Content
@extensionfor App
@since 3.6.0
**/
function AppContent() {
PjaxContent.apply(this, arguments);
}
AppContent.prototype = {
// -- Public Methods -------------------------------------------------------
serverContentRoute: function (view, config, options, callback) {
var args = arguments,
self = this;
return function (req, res, next) {
var viewInfo = Lang.isString(view) && self.getViewInfo(view);
// If we're suppose to show a preserved view, just show it
if (viewInfo && viewInfo.preserve && viewInfo.instance) {
return self.showView.apply(self, args);
}
res.showContent = function (node) {
var container;
if (node.isFragment()) {
if (node.get('children').size() === 1) {
container = node.get('firstChild');
} else {
container = Y.Node.create('<div/>');
container.append(node);
}
} else {
container = node;
}
view || (view = '');
config || (config = {});
config.container = container;
self.showView(view, config, options, callback);
};
self._defaultRoute(req, res, next);
};
},
// -- Event Handlers -------------------------------------------------------
/**
Default event handler for both the `error` and `load` events. Attempts to
insert the loaded content into the `container` node and update the page's
title.
@method _defCompleteFn
@param {EventFacade} e
@protected
@since 3.5.0
**/
_defCompleteFn: function (e) {
var content = e.content,
view, activeViewHandle;
if (!content.node) { return; }
if (content.title && Y.config.doc) {
// Make sure the `activeView` does actually change before we go
// messing with the page title.
activeViewHandle = this.onceAfter('activeViewChange', function () {
Y.config.doc.title = content.title;
});
}
e.route.res.showContent(content.node);
// Detach the handle just in case.
activeViewHandle.detach();
}
};
// Mix statics.
Y.mix(AppContent, PjaxContent);
// Mix prototype.
Y.mix(AppContent, PjaxContent, false, null, 1);
// -- Namespace ----------------------------------------------------------------
Y.App.Content = AppContent;
Y.Base.mix(Y.App, [AppContent]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment