Skip to content

Commit

Permalink
Merge pull request selectize#276 from dguenther/clear_cache
Browse files Browse the repository at this point in the history
Added a clearCache method to clear the render cache
  • Loading branch information
brianreavis committed Jun 1, 2014
2 parents e1e65c7 + 0e093ea commit c41c2d4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ var selectize = $select[0].selectize;
<td valign="top"><code>isFull()</code></td>
<td valign="top">Returns whether or not the user can select more items.</td>
</tr>
<tr>
<td valign="top"><code>clearCache(template)</code></td>
<td valign="top">Clears the render cache. Takes an optional template argument (e.g. "option", "item") to clear only that cache.</td>
</tr>
</table>

### Related Objects
Expand Down
17 changes: 17 additions & 0 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,23 @@ $.extend(Selectize.prototype, {
}

return html;
},

/**
* Clears the render cache for a template. If
* no template is given, clears all render
* caches.
*
* @param {string} templateName
*/
clearCache: function(templateName) {
var self = this;
if (typeof templateName === 'undefined') {
self.renderCache = {};
} else {
delete self.renderCache[templateName];
}
}


});
35 changes: 35 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,41 @@
});
});

describe('clearCache()', function() {
var test;

before(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 2},
{value: 3},
],
items: ['1','2','3']
});
test.selectize.advanceSelection(1);
test.selectize.refreshOptions(true);
test.selectize.refreshItems();
});
it('should clear the whole renderCache', function () {
expect($.isEmptyObject(test.selectize.renderCache)).to.be.equal(false);
test.selectize.clearCache();
expect($.isEmptyObject(test.selectize.renderCache)).to.be.equal(true);
});
it('should allow clearing just one template type from the renderCache', function () {
test.selectize.render('item', test.selectize.options[0]);
test.selectize.refreshOptions();
expect($.isEmptyObject(test.selectize.renderCache['option'])).to.be.equal(false);
expect($.isEmptyObject(test.selectize.renderCache['item'])).to.be.equal(false);
test.selectize.clearCache('option');
expect($.isEmptyObject(test.selectize.renderCache['option'])).to.be.equal(true);
expect($.isEmptyObject(test.selectize.renderCache['item'])).to.be.equal(false);
});
});

});

})();

0 comments on commit c41c2d4

Please sign in to comment.