Skip to content

Commit

Permalink
Change the cache and add dispose() method
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Sep 6, 2014
1 parent 8ca8fc2 commit 5687e51
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
29 changes: 20 additions & 9 deletions dropkick.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ window.isIframe = (window.parent != window.self && location.host === parent.loca
var

// Cache of DK Objects
dkCache = [],
dkCache = {},
dkIndex = 0,

// The Dropkick Object
Dropkick = function( sel, opts ) {
Expand Down Expand Up @@ -245,7 +246,7 @@ Dropkick.prototype = {
*/
init: function( sel, opts ) {
var i,
dk = Dropkick.build( sel, "dk" + dkCache.length );
dk = Dropkick.build( sel, "dk" + dkIndex );

// Set some data on the DK Object
this.data = {};
Expand Down Expand Up @@ -281,21 +282,24 @@ Dropkick.prototype = {
}
}

if ( dkCache.length === 0 ) {
if ( dkIndex === 0 ) {
document.addEventListener( "click", Dropkick.onDocClick );
if ( window.isIframe ){
parent.document.addEventListener( "click", Dropkick.onDocClick );
}
}

// Add the DK Object to the cache
this.data.cacheID = dkCache.length;
this.data.cacheID = dkIndex;
sel.setAttribute( "data-dkCacheId", this.data.cacheID );
dkCache.push( this );
dkCache[ this.data.cacheID ] = this;

// Call the optional initialize function
this.data.settings.initialize.call( this );

// Increment the index
dkIndex += 1;

return this;
},

Expand Down Expand Up @@ -537,11 +541,18 @@ Dropkick.prototype = {
* (use if HTMLSelectElement has changed)
*/
refresh: function() {
dkCache.splice( this.data.cacheID, 1 );
this.data.elem.parentNode.removeChild( this.data.elem );
this.init( this.data.select, this.data.settings );
this.dispose().init( this.data.select, this.data.settings );
},

/**
* Removes the DK Object from the cache and the element from the DOM
*/
dispose: function() {
delete dkCache[ this.data.cachID ];
this.data.elem.parentNode.removeChild( this.data.elem );
this.data.select.removeAttribute( "data-dkCacheId" );
return this;

This comment has been minimized.

Copy link
@Robdel12

Robdel12 Sep 6, 2014

Owner

👍 for returning this

},

// Private Methods

Expand Down Expand Up @@ -853,7 +864,7 @@ Dropkick.onDocClick = function( event ) {
}
}

for ( i = 0; i < dkCache.length; i++ ) {
for ( i in dkCache ) {
if ( !_.closest( event.target, dkCache[ i ].data.elem ) ) {
dkCache[ i ].disabled || dkCache[ i ].close();
}
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ Documentation
Rebuilds the Dropkick and reinitalized the Dropkick object. *Only use if the
original select element has changed.

- dk.**dispose**()

Removes the Dropkick element from the DOM and the object from the cache.

Returns itself.

## Advanced

These are categorized as "advanced" because we feel as these wouldn't normally
Expand Down

2 comments on commit 5687e51

@rjft197
Copy link

@rjft197 rjft197 commented on 5687e51 Sep 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's probably bad on my behalf but I was relying on the numbers of the DK elements that were created to run some custom css and JS on them. Keeping the same cache ID would have been helpful. Hopefully I can make a container around the entire select box and then reference the children elements from there, however I don't think that's as tidy.

@wwilsman
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's of any help, any classes you add to the select will also be added to the Dropkick element. So instead of adding a container, you can just add a class (of course the Dropkick element will also have a dk-select or dk-multiselect class).

If you're adding the selects programmatically, you can keep a ticker and for every select add a class of 'select-' + ticker++

Please # to comment.