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('tree-lazy', function (Y, NAME) {
10 /*jshint expr:true, maxlen:200, onevar:false */
13 Provides `Plugin.Tree.Lazy`, a plugin for `Tree.Openable` that makes it easy to
14 lazily load and populate the contents of tree nodes the first time they're
22 A plugin for `Tree.Openable` that makes it easy to lazily load and populate the
23 contents of tree nodes the first time they're opened.
27 YUI().use('jsonp', 'tree-openable', 'tree-lazy', function (Y) {
28 var Tree = Y.Base.create('openableTree', Y.Tree, [Y.Tree.Openable]),
31 tree.plug(Y.Plugin.Tree.Lazy, {
33 // Custom function that Plugin.Tree.Lazy will call when it needs to
34 // load the children for a node.
35 load: function (node, callback) {
36 // Request the data for this node's children via JSONP.
37 Y.jsonp('http://example.com/api/data?callback={callback}', function (data) {
38 // If we didn't get any data back, treat this as an error.
40 callback(new Error('No data!'));
44 // Append the children to the node (assume `data.children` is
45 // an array of child node data for the sake of this example).
46 node.append(data.children);
48 // Call the callback function to tell Plugin.Tree.Lazy that
49 // we're done loading data.
57 @class Plugin.Tree.Lazy
58 @param {Object} config Config object.
60 @param {Function} config.load Custom `load()` function that will be called
61 when a node's children need to be loaded. This function must call the
62 provided callback to indicate completion.
64 @param {Function} config.load.callback Callback function. The custom
65 `load()` function must call this callback to indicate completion.
67 @param {Error} [config.load.callback.err] Error object. If provided,
68 the load action will be considered a failure, and an `error`
69 event will be fired. Omit this argument (or set it to `null`) to
77 Fired just before the custom `load()` method is called to load child nodes for a
80 Calling `preventDefault()` on this event's facade will cancel the load action
81 and prevent the `load()` method from being called.
84 @param {Tree.Node} node Tree node whose children will be loaded.
85 @preventable _defBeforeLoadFn
87 var EVT_BEFORE_LOAD = 'beforeLoad';
90 Fired when the `load()` method indicates there was an error loading child nodes.
93 @param {Error} error Error provided by the `load()` method.
94 @param {String} src Source of the error (defaults to "load").
96 var EVT_ERROR = 'error';
99 Fired after child nodes have finished loading and have been added to the tree.
102 @param {Tree.Node} node Tree node whose children have been loaded.
104 var EVT_LOAD = 'load';
106 Y.namespace('Plugin.Tree').Lazy = Y.Base.create('lazyTreePlugin', Y.Plugin.Base, [], {
107 // -- Lifecycle Methods ----------------------------------------------------
108 initializer: function (config) {
109 this._host = config.host;
112 this.load = config.load;
115 // Make sure we've been plugged into a Tree that mixes in the
116 // Tree.Openable extension.
117 if (!this._host.openNode) {
118 Y.log("Plugin.Tree.Lazy was plugged into a Tree that doesn't mix in the Tree.Openable extension. This probably won't do you much good.", 'warn', 'tree-lazy');
121 this._published = {};
122 this._attachEvents();
125 // -- Public Methods -------------------------------------------------------
126 load: function (node, callback) {
127 callback(new Error('Plugin.Tree.Lazy: Please provide a custom `load` method when instantiating this plugin.'));
130 // -- Protected Methods ----------------------------------------------------
131 _attachEvents: function () {
132 this.onHostEvent('open', this._onOpen);
135 // -- Protected Event Handlers ---------------------------------------------
136 _onOpen: function (e) {
139 // Nothing to do if this node can't have children or if its children
140 // have already been (or are already being) loaded.
141 if (!node.canHaveChildren || node.state.loaded || node.state.loading) {
145 if (!this._published[EVT_BEFORE_LOAD]) {
146 this._published[EVT_BEFORE_LOAD] = this.publish(EVT_BEFORE_LOAD, {
147 defaultFn: this._defLoadingFn
151 this.fire(EVT_BEFORE_LOAD, {node: node});
154 // -- Default Event Handlers -----------------------------------------------
155 _defLoadingFn: function (e) {
159 node.state.loading = true;
161 this.load(node, function (err) {
162 delete node.state.loading;
165 self.fire(EVT_ERROR, {
173 node.state.loaded = true;
175 self.fire(EVT_LOAD, {node: node});
183 }, '3.13.0', {"requires": ["base-pluginhost", "plugin", "tree"]});