1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('view-node-map', function (Y, NAME) {
5
View extension that adds a static `getByNode()` method that returns the nearest
6
View instance associated with the given Node (similar to Widget's `getByNode()`
10
@submodule view-node-map
14
var buildCfg = Y.namespace('View._buildCfg'),
18
View extension that adds a static `getByNode()` method that returns the nearest
19
View instance associated with the given Node (similar to Widget's `getByNode()`
22
Note that it's important to call `destroy()` on a View instance using this
23
extension when you plan to stop using it. This ensures that all internal
24
references to that View are cleared to prevent memory leaks.
32
// Tells Base.create() to mix the static getByNode method into built classes.
33
// We're cheating and modifying Y.View here, because right now there's no better
35
buildCfg.aggregates || (buildCfg.aggregates = []);
36
buildCfg.aggregates.push('getByNode');
39
Returns the nearest View instance associated with the given Node. The Node may
40
be a View container or any child of a View container.
42
Note that only instances of Views that have the Y.View.NodeMap extension mixed
43
in will be returned. The base View class doesn't provide this functionality by
44
default due to the additional memory management overhead involved in maintaining
45
a mapping of Nodes to View instances.
48
@param {Node|HTMLElement|String} node Node instance, selector string, or
50
@return {View} Closest View instance associated with the given Node, or `null`
51
if no associated View instance was found.
55
NodeMap.getByNode = function (node) {
58
Y.one(node).ancestor(function (ancestor) {
59
return (view = instances[Y.stamp(ancestor, true)]) || false;
65
// To make this testable.
66
NodeMap._instances = instances;
69
initializer: function () {
70
instances[Y.stamp(this.get('container'))] = this;
73
destructor: function () {
74
var stamp = Y.stamp(this.get('container'), true);
76
if (stamp in instances) {
77
delete instances[stamp];
82
Y.View.NodeMap = NodeMap;
85
}, '3.9.1', {"requires": ["view"]});