Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
93043d00
Commit
93043d00
authored
Feb 13, 2013
by
Rick Waldron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor: Data.prototype.access. Thanks to @RubyLouvre and @gibson042. Closes #1167
parent
b734666f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
24 deletions
+51
-24
data.js
src/data.js
+25
-16
data.js
test/unit/data.js
+26
-8
No files found.
src/data.js
View file @
93043d00
...
@@ -108,24 +108,33 @@ Data.prototype = {
...
@@ -108,24 +108,33 @@ Data.prototype = {
cache
:
cache
[
key
];
cache
:
cache
[
key
];
},
},
access
:
function
(
owner
,
key
,
value
)
{
access
:
function
(
owner
,
key
,
value
)
{
if
(
value
===
undefined
&&
(
key
&&
typeof
key
!==
"object"
)
)
{
// In cases where either:
// Assume this is a request to read the cached data
//
// 1. No key was specified
// 2. A string key was specified, but no value provided
//
// Take the "read" path and allow the get method to determine
// which value to return, respectively either:
//
// 1. The entire cache object
// 2. The data stored at the key
//
if
(
key
===
undefined
||
((
key
&&
typeof
key
===
"string"
)
&&
value
===
undefined
)
)
{
return
this
.
get
(
owner
,
key
);
return
this
.
get
(
owner
,
key
);
}
else
{
// If only an owner was specified, return the entire
// cache object.
if
(
key
===
undefined
)
{
return
this
.
get
(
owner
);
}
// Allow setting or extending (existing objects) with an
// object of properties, or a key and val
this
.
set
(
owner
,
key
,
value
);
return
value
!==
undefined
?
value
:
key
;
}
}
// Otherwise, this is a read request.
return
this
.
get
(
owner
,
key
);
// [*]When the key is not a string, or both a key and value
// are specified, set or extend (existing objects) with either:
//
// 1. An object of properties
// 2. A key and value
//
this
.
set
(
owner
,
key
,
value
);
// Since the "set" path can have two possible entry points
// return the expected data based on which path was taken[*]
return
value
!==
undefined
?
value
:
key
;
},
},
remove
:
function
(
owner
,
key
)
{
remove
:
function
(
owner
,
key
)
{
var
i
,
l
,
name
,
var
i
,
l
,
name
,
...
...
test/unit/data.js
View file @
93043d00
...
@@ -7,29 +7,47 @@ test("expando", function(){
...
@@ -7,29 +7,47 @@ test("expando", function(){
});
});
test
(
"jQuery.data & removeData, expected returns"
,
function
()
{
test
(
"jQuery.data & removeData, expected returns"
,
function
()
{
expect
(
2
);
expect
(
4
);
var
elem
=
document
.
body
;
equal
(
equal
(
jQuery
.
data
(
document
.
body
,
"hello"
,
"world"
),
"world"
,
jQuery
.
data
(
elem
,
"hello"
,
"world"
),
"world"
,
"jQuery.data( elem, key, value ) returns value"
"jQuery.data( elem, key, value ) returns value"
);
);
equal
(
equal
(
jQuery
.
removeData
(
document
.
body
,
"hello"
),
undefined
,
jQuery
.
data
(
elem
,
"hello"
),
"world"
,
"jQuery.data( elem, key ) returns value"
);
deepEqual
(
jQuery
.
data
(
elem
,
{
goodnight
:
"moon"
}),
{
goodnight
:
"moon"
},
"jQuery.data( elem, key, obj ) returns obj"
);
equal
(
jQuery
.
removeData
(
elem
,
"hello"
),
undefined
,
"jQuery.removeData( elem, key, value ) returns undefined"
"jQuery.removeData( elem, key, value ) returns undefined"
);
);
});
});
test
(
"jQuery._data & _removeData, expected returns"
,
function
()
{
test
(
"jQuery._data & _removeData, expected returns"
,
function
()
{
expect
(
2
);
expect
(
4
);
var
elem
=
document
.
body
;
equal
(
equal
(
jQuery
.
_data
(
document
.
body
,
"hello"
,
"world"
),
"world"
,
jQuery
.
_data
(
elem
,
"hello"
,
"world"
),
"world"
,
"jQuery.data( elem, key, value ) returns value"
"jQuery.
_
data( elem, key, value ) returns value"
);
);
equal
(
equal
(
jQuery
.
_removeData
(
document
.
body
,
"hello"
),
undefined
,
jQuery
.
_data
(
elem
,
"hello"
),
"world"
,
"jQuery.removeData( elem, key, value ) returns undefined"
"jQuery._data( elem, key ) returns value"
);
deepEqual
(
jQuery
.
_data
(
elem
,
{
goodnight
:
"moon"
}),
{
goodnight
:
"moon"
},
"jQuery._data( elem, obj ) returns obj"
);
equal
(
jQuery
.
_removeData
(
elem
,
"hello"
),
undefined
,
"jQuery._removeData( elem, key, value ) returns undefined"
);
);
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment