Skip to content

Commit

Permalink
Data: Combine register and cache methods
Browse files Browse the repository at this point in the history
Closes gh-2553
  • Loading branch information
jbedard authored and mgol committed Sep 14, 2015
1 parent ce3b4a6 commit b5f7c9e
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions src/data/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b5f7c9e

Please sign in to comment.