Permalink
Browse files

Data: Combine register and cache methods

Closes gh-2553
  • Loading branch information...
jbedard authored and mgol committed Aug 17, 2015
1 parent ce3b4a6 commit b5f7c9e2d526b17b9962976bb704dce8779d7362
Showing with 27 additions and 33 deletions.
  1. +27 −33 src/data/Data.js
View
@@ -12,44 +12,38 @@ Data.uid = 1;
Data.prototype = {
register: function( owner ) {
var value = {};
// If it is a node unlikely to be stringify-ed or looped over
// use plain assignment
if ( owner.nodeType ) {
owner[ this.expando ] = value;
// Otherwise secure it in a non-enumerable property
// configurable must be true to allow the property to be
// deleted when data is removed
} else {
Object.defineProperty( owner, this.expando, {
value: value,
configurable: true
} );
}
return owner[ this.expando ];
},
cache: function( owner ) {
// We can accept data for non-element nodes in modern browsers,
// but we should not, see #8335.
// Always return an empty object.
if ( !acceptData( owner ) ) {
return {};
}
// Check if the owner object already has a cache
var cache = owner[ this.expando ];
// If so, return it
if ( cache ) {
return cache;
var value = owner[ this.expando ];
// If not, create one
if ( !value ) {
value = {};
// We can accept data for non-element nodes in modern browsers,
// but we should not, see #8335.
// Always return an empty object.
if ( acceptData( owner ) ) {
// If it is a node unlikely to be stringify-ed or looped over
// use plain assignment
if ( owner.nodeType ) {
owner[ this.expando ] = value;
// Otherwise secure it in a non-enumerable property
// configurable must be true to allow the property to be
// deleted when data is removed
} else {
Object.defineProperty( owner, this.expando, {
value: value,
configurable: true
} );
}
}
}
// If not, register one
return this.register( owner );
return value;
},
set: function( owner, data, value ) {
var prop,

0 comments on commit b5f7c9e

Please sign in to comment.