2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('app-content', function (Y, NAME) {
11
`Y.App` extension that provides pjax-style content fetching and handling.
14
@submodule app-content
18
var PjaxContent = Y.PjaxContent;
21
`Y.App` extension that provides pjax-style content fetching and handling.
23
This makes it easy to fetch server rendered content for URLs using Ajax. The
24
HTML content returned from the server will be view-ified and set as the app's
25
main content, making it seamless to use a mixture of server and client rendered
28
When the `"app-content"` module is used, it will automatically mix itself into
29
`Y.App`, and it provides three main features:
31
- **`Y.App.Content.route`**: A stack of middleware which forms a pjax-style
34
- **`loadContent()`**: Route middleware which load content from a server. This
35
makes an Ajax request for the requested URL, parses the returned content and
36
puts it on the route's response object.
38
- **`showContent()`**: Method which provides an easy way to view-ify HTML
39
content which should be shown as an app's active/visible view.
41
The following is an example of how these features can be used:
43
// Creates a new app and registers the `"post"` view.
46
post: {type: Y.PostView}
50
// Uses a simple server rendered content route for the About page.
51
app.route('/about/', Y.App.Content.route);
53
// Uses the `loadContent()` middleware to fetch the contents of the post
54
// from the server and shows that content in a `"post"` view.
55
app.route('/posts/:id/', 'loadContent', function (req, res, next) {
56
this.showContent(res.content.node, {view: 'post'});
64
function AppContent() {
65
PjaxContent.apply(this, arguments);
69
A stack of middleware which forms a pjax-style content route.
71
This route will load the rendered HTML content from the server, then create and
72
show a new view using those contents.
79
AppContent.route = ['loadContent', '_contentRoute'];
81
AppContent.prototype = {
82
// -- Public Methods -------------------------------------------------------
85
Sets this app's `activeView` attribute using the specified `content`.
87
This provides an easy way to view-ify HTML content which should be shown as
88
this app's active/visible view. This method will determine the appropriate
89
view `container` node based on the specified `content`. By default, a new
90
`Y.View` instance will be created unless `options.view` is specified.
92
Under the hood, this method calls the `showView()` method, so refer to its
93
docs for more information.
96
@param {HTMLElement|Node|String} content The content to show, it may be
97
provided as a selector string, a DOM element, or a `Y.Node` instance.
98
@param {Object} [options] Optional objects containing any of the following
99
properties in addition to any `showView()` options:
101
@param {Object|String} [options.view] The name of a view defined in this
102
app's `views`, or an object with the following properties:
104
@param {String} options.view.name The name of a view defined in this
106
@param {Object} [options.view.config] Optional configuration to use when
107
creating the new view instance. This config object can also be used
108
to update an existing or preserved view's attributes when
109
`options.update` is `true`. **Note:** If a `container` is specified,
110
it will be overridden by the `content` specified in the first
113
@param {Function} [callback] Optional callback function to call after the
114
new `activeView` is ready to use. **Note:** this will override
115
`options.callback` and it can be specified as either the second or third
116
argument. The function will be passed the following:
118
@param {View} callback.view A reference to the new `activeView`.
123
showContent: function (content, options, callback) {
124
// Makes sure we have a node instance, and will query selector strings.
125
content = Y.one(content);
127
// Support the callback function being either the second or third arg.
128
if (typeof options === 'function') {
129
options = {callback: options};
133
// Mix in default option to *not* render the view because presumably we
134
// have pre-rendered content here. This also creates a copy so we can
135
// modify the object.
136
options = Y.merge({render: false}, options);
138
var view = options.view || '',
139
viewName = typeof view === 'string' ? view : view.name,
140
viewConfig = typeof view !== 'string' ? view.config : {},
141
viewInfo = this.getViewInfo(viewName),
142
container, template, type, ViewConstructor;
144
// Remove `view` from the `options` which will be passed along to the
145
// `showView()` method.
148
// When the specified `content` is a document fragment, we want to see
149
// if it only contains a single node, and use that as the content. This
150
// checks `childNodes` which will include text nodes.
151
if (content && content.isFragment() &&
152
content.get('childNodes').size() === 1) {
154
content = content.get('firstChild');
157
// When the `content` is an element node (`nodeType` 1), we can use it
158
// as-is for the `container`. Otherwise, we'll construct a new container
159
// based on the `options.view`'s `containerTemplate`.
160
if (content && content.get('nodeType') === 1) {
163
type = (viewInfo && viewInfo.type) || Y.View;
165
// Looks for a namespaced constructor function on `Y`.
166
ViewConstructor = typeof type === 'string' ?
167
Y.Object.getValue(Y, type.split('.')) : type;
169
// Find the correct node template for the view.
170
template = ViewConstructor.prototype.containerTemplate;
171
container = Y.Node.create(template);
173
// Append the document fragment to the newly created `container`
174
// node. This is the worst case where we have to create a wrapper
175
// node around the `content`.
176
container.append(content);
179
// Makes sure the view is created using _our_ `container` node.
180
viewConfig = Y.merge(viewConfig, {container: container});
182
// Finally switch to the new `activeView`. We want to make sure `view`
183
// is a string if it's falsy, that way a new view will be created.
184
return this.showView(viewName, viewConfig, options, callback);
187
// -- Protected Methods ----------------------------------------------------
190
Provides a default content route which will show a server rendered view.
192
**Note:** This route callback assumes that it's called after the
193
`loadContent()` middleware.
195
@method _contentRoute
196
@param {Object} req Request object.
197
@param {Object} res Response Object.
198
@param {Function} next Function to pass control to the next route callback.
201
@see Y.App.Content.route
203
_contentRoute: function (req, res, next) {
204
var content = res.content,
208
// We must have some content to work with.
209
if (!(content && content.node)) { return next(); }
211
if (content.title && doc) {
212
// Make sure the `activeView` does actually change before we go
213
// messing with the page title.
214
activeViewHandle = this.onceAfter('activeViewChange', function () {
215
doc.title = content.title;
219
this.showContent(content.node);
221
// Detach the handle just in case.
222
if (activeViewHandle) {
223
activeViewHandle.detach();
231
AppContent.ATTRS = Y.Attribute.protectAttrs(PjaxContent.ATTRS);
234
Y.mix(AppContent, PjaxContent, false, null, 1);
236
// -- Namespace ----------------------------------------------------------------
237
Y.App.Content = AppContent;
238
Y.Base.mix(Y.App, [AppContent]);
241
}, '3.10.3', {"requires": ["app-base", "pjax-content"]});