Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Avoid side-effects when calling jQuery.hasData
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
- Loading branch information
Showing
2 changed files
with
19 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,17 @@ function Data() { | |
Data.uid = 1; | ||
|
||
Data.prototype = { | ||
key: function( owner ) { | ||
key: function( owner, options ) { | ||
var descriptor = {}, | ||
// Check if the owner object already has a cache key | ||
unlock = owner[ this.expando ]; | ||
|
||
// `readonly` calls from hasData, on owners with no key | ||
// should not create new/empty cache records | ||
if ( !unlock && (options && options.readonly) ) { | ||
return null; | ||
} | ||
|
||
// If not, create one | ||
if ( !unlock ) { | ||
unlock = Data.uid++; | ||
|
@@ -158,7 +164,7 @@ Data.prototype = { | |
}, | ||
hasData: function( owner ) { | ||
return !jQuery.isEmptyObject( | ||
this.cache[ this.key( owner ) ] | ||
this.cache[ this.key( owner, { readonly: true }) ] || {} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rwaldron
Author
Member
|
||
); | ||
}, | ||
discard: function( owner ) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@rwldrn I assume it's this way for a reason, but why not leave
.key
alone and bring.hasData
up next to it likereturn !jQuery.isEmptyObject( owner[ this.expando ] || {} )
? Redundant, admittedly, but also smaller and faster.