/
jquery.ba-getobject.js
177 lines (159 loc) · 5.67 KB
/
jquery.ba-getobject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*!
* jQuery getObject - v1.1 - 12/24/2009
* http://benalman.com/projects/jquery-getobject-plugin/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*
* Inspired by Dojo, which is Copyright (c) 2005-2009, The Dojo Foundation.
*/
// Script: jQuery getObject: get.and.set.deep.objects.easily = true;
//
// *Version: 1.1, Last updated: 12/24/2009*
//
// Project Home - http://benalman.com/projects/jquery-getobject-plugin/
// GitHub - http://github.com/cowboy/jquery-getobject/
// Source - http://github.com/cowboy/jquery-getobject/raw/master/jquery.ba-getobject.js
// (Minified) - http://github.com/cowboy/jquery-getobject/raw/master/jquery.ba-getobject.min.js (0.7kb)
//
// About: License
//
// Copyright (c) 2009 "Cowboy" Ben Alman,
// Dual licensed under the MIT and GPL licenses.
// http://benalman.com/about/license/
//
// Inspired by Dojo, which is Copyright (c) 2005-2009, The Dojo Foundation.
//
// About: Support and Testing
//
// Information about what version or versions of jQuery this plugin has been
// tested with, what browsers it has been tested in, and where the unit tests
// reside (so you can test it yourself).
//
// jQuery Versions - none, 1.3.2, 1.4a2
// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.
// Unit Tests - http://benalman.com/code/projects/jquery-getobject/unit/
//
// About: Release History
//
// 1.1 - (12/24/2009) Removed dependency on jQuery. Now creates methods in a
// `Cowboy` namespace if jQuery isn't present.
// 1.0 - (12/19/2009) Initial release
//
// Topic: Note for non-jQuery users
//
// jQuery isn't actually required for this plugin, because nothing internal
// uses any jQuery methods or properties. jQuery is just used as a namespace
// under which these methods can exist.
//
// Since jQuery isn't actually required for this plugin, if jQuery doesn't exist
// when this plugin is loaded, the methods described below will be created in
// the `Cowboy` namespace. Usage will be exactly the same, but instead of
// $.method() or jQuery.method(), you'll need to use Cowboy.method().
//
// Topic: Note for Dojo users
//
// The setObject, getObject, and exists methods are similar to their Dojo
// counterparts, with the exception that exists returns true or false based
// on whether or not a property is defined, not whether it is truthy.
(function(window,undefined){
'$:nomunge'; // Used by YUI compressor.
// Since jQuery really isn't required for this plugin, use `jQuery` as the
// namespace only if it already exists, otherwise use the `Cowboy` namespace,
// creating it if necessary.
var $ = window.jQuery || window.Cowboy || ( window.Cowboy = {} ),
// Method reference.
jq_getObject;
// Method: jQuery.getObject
//
// Get a property of an object via dot-delimited name string, and optionally
// create the property and any ancestor properties that do not already exist.
//
// Usage:
//
// > jQuery.getObject( name [, create ] [, context ] );
//
// Arguments:
//
// name - (String) Dot-delimited string representing a property name, for
// example: 'document', 'location.href', 'window.open' or 'foo.bar.baz'.
// create - (Boolean) Create final and intermediate properties if they don't
// exist. Defaults to false.
// context - (Object) Optional context in which to evaluate name. Defaults to
// window if omitted.
//
// Returns:
//
// (Object) An object reference or value on success, otherwise undefined.
$.getObject = jq_getObject = function( parts, create, obj ) {
if ( typeof parts === 'string' ) {
parts = parts.split('.');
}
if ( typeof create !== 'boolean' ) {
obj = create;
create = undefined;
}
obj = obj || window;
var p;
while ( obj && parts.length ) {
p = parts.shift();
if ( obj[p] === undefined && create ) {
obj[p] = {};
}
obj = obj[p];
}
return obj;
};
// Method: jQuery.setObject
//
// Set a property of an object via dot-delimited name string, creating any
// ancestor properties that do not already exist.
//
// Usage:
//
// > jQuery.setObject( name, value, [ context ] );
//
// Arguments:
//
// name - (String) Dot-delimited string representing a property name, for
// example: 'document', 'location.href', 'window.open' or 'foo.bar.baz'.
// value - (Anything) Any valid JavaScript expression.
// context - (Object) Optional context in which to evaluate name. Defaults to
// window if omitted.
//
// Returns:
//
// (Anything) The value if set successfully, otherwise undefined.
$.setObject = function( name, value, context ) {
var parts = name.split('.'),
prop = parts.pop(),
obj = jq_getObject( parts, true, context );
// Only return the value if it is set successfully.
return obj && typeof obj === 'object' && prop
? ( obj[prop] = value )
: undefined;
};
// Method: jQuery.exists
//
// Using dot-delimited name string, return whether a property of an object
// exists.
//
// Usage:
//
// > jQuery.exists( name [, context ] );
//
// Arguments:
//
// name - (String) Dot-delimited string representing a property name, for
// example: 'document', 'location.href', 'window.open' or 'foo.bar.baz'.
// context - (Object) Optional context in which to evaluate name. Defaults to
// window if omitted.
//
// Returns:
//
// (Boolean) Whether or not the property exists.
$.exists = function( name, context ) {
return jq_getObject( name, context ) !== undefined;
};
})(this);