2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('pjax', function (Y, NAME) {
11 Provides seamless, gracefully degrading Pjax (pushState + Ajax) functionality,
12 which makes it easy to progressively enhance standard links on the page so that
13 they can be loaded normally in old browsers, or via Ajax (with HTML5 history
14 support) in newer browsers.
22 A stack of middleware which forms the default Pjax route.
24 @property defaultRoute
29 var defaultRoute = ['loadContent', '_defaultRoute'],
32 Fired when an error occurs while attempting to load a URL via Ajax.
35 @param {Object} content Content extracted from the response, if any.
36 @param {Node} content.node A `Y.Node` instance for a document fragment
37 containing the extracted HTML content.
38 @param {String} [content.title] The title of the HTML page, if any,
39 extracted using the `titleSelector` attribute. If `titleSelector` is
40 not set or if a title could not be found, this property will be
42 @param {String} responseText Raw Ajax response text.
43 @param {Number} status HTTP status code for the Ajax response.
44 @param {String} url The absolute URL that failed to load.
50 Fired when a URL is successfully loaded via Ajax.
53 @param {Object} content Content extracted from the response, if any.
54 @param {Node} content.node A `Y.Node` instance for a document fragment
55 containing the extracted HTML content.
56 @param {String} [content.title] The title of the HTML page, if any,
57 extracted using the `titleSelector` attribute. If `titleSelector` is
58 not set or if a title could not be found, this property will be
60 @param {String} responseText Raw Ajax response text.
61 @param {Number} status HTTP status code for the Ajax response.
62 @param {String} url The absolute URL that was loaded.
68 Provides seamless, gracefully degrading Pjax (pushState + Ajax) functionality,
69 which makes it easy to progressively enhance standard links on the page so that
70 they can be loaded normally in old browsers, or via Ajax (with HTML5 history
71 support) in newer browsers.
78 @param {Object} [config] Config attributes.
81 Y.Pjax = Y.Base.create('pjax', Y.Router, [Y.PjaxBase, Y.PjaxContent], {
82 // -- Lifecycle Methods ----------------------------------------------------
83 initializer: function () {
84 this.publish(EVT_ERROR, {defaultFn: this._defCompleteFn});
85 this.publish(EVT_LOAD, {defaultFn: this._defCompleteFn});
88 // -- Protected Methods ----------------------------------------------------
91 Default Pjax route callback. Fires either the `load` or `error` event based
92 on the status of the `Y.io` request made by the `loadContent()` middleware.
94 **Note:** This route callback assumes that it's called after the
95 `loadContent()` middleware.
98 @param {Object} req Request object.
99 @param {Object} res Response Object.
100 @param {Function} next Function to pass control to the next route callback.
103 @see Y.Pjax.defaultRoute
105 _defaultRoute: function (req, res, next) {
106 var ioResponse = res.ioResponse,
107 status = ioResponse.status,
108 event = status >= 200 && status < 300 ? EVT_LOAD : EVT_ERROR;
111 content : res.content,
112 responseText: ioResponse.responseText,
120 // -- Event Handlers -------------------------------------------------------
123 Default event handler for both the `error` and `load` events. Attempts to
124 insert the loaded content into the `container` node and update the page's
127 @method _defCompleteFn
128 @param {EventFacade} e
132 _defCompleteFn: function (e) {
133 var container = this.get('container'),
136 if (container && content.node) {
137 container.setHTML(content.node);
140 if (content.title && Y.config.doc) {
141 Y.config.doc.title = content.title;
147 Node into which content should be inserted when a page is loaded via
148 Pjax. This node's existing contents will be removed to make way for the
151 If not set, loaded content will not be automatically inserted into the
164 // Inherited from Router and already documented there.
167 {path: '*', callbacks: defaultRoute}
172 // Documented towards the top of this file.
173 defaultRoute: defaultRoute
177 }, '3.13.0', {"requires": ["pjax-base", "pjax-content"]});