Skip to content

Commit c41c2d4

Browse files
committed
Merge pull request selectize#276 from dguenther/clear_cache
Added a clearCache method to clear the render cache
2 parents e1e65c7 + 0e093ea commit c41c2d4

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

docs/api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ var selectize = $select[0].selectize;
195195
<td valign="top"><code>isFull()</code></td>
196196
<td valign="top">Returns whether or not the user can select more items.</td>
197197
</tr>
198+
<tr>
199+
<td valign="top"><code>clearCache(template)</code></td>
200+
<td valign="top">Clears the render cache. Takes an optional template argument (e.g. "option", "item") to clear only that cache.</td>
201+
</tr>
198202
</table>
199203

200204
### Related Objects

src/selectize.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,23 @@ $.extend(Selectize.prototype, {
19131913
}
19141914

19151915
return html;
1916+
},
1917+
1918+
/**
1919+
* Clears the render cache for a template. If
1920+
* no template is given, clears all render
1921+
* caches.
1922+
*
1923+
* @param {string} templateName
1924+
*/
1925+
clearCache: function(templateName) {
1926+
var self = this;
1927+
if (typeof templateName === 'undefined') {
1928+
self.renderCache = {};
1929+
} else {
1930+
delete self.renderCache[templateName];
1931+
}
19161932
}
19171933

1934+
19181935
});

test/api.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,41 @@
567567
});
568568
});
569569

570+
describe('clearCache()', function() {
571+
var test;
572+
573+
before(function() {
574+
test = setup_test('<select multiple>', {
575+
valueField: 'value',
576+
labelField: 'value',
577+
options: [
578+
{value: 0},
579+
{value: 1},
580+
{value: 2},
581+
{value: 3},
582+
],
583+
items: ['1','2','3']
584+
});
585+
test.selectize.advanceSelection(1);
586+
test.selectize.refreshOptions(true);
587+
test.selectize.refreshItems();
588+
});
589+
it('should clear the whole renderCache', function () {
590+
expect($.isEmptyObject(test.selectize.renderCache)).to.be.equal(false);
591+
test.selectize.clearCache();
592+
expect($.isEmptyObject(test.selectize.renderCache)).to.be.equal(true);
593+
});
594+
it('should allow clearing just one template type from the renderCache', function () {
595+
test.selectize.render('item', test.selectize.options[0]);
596+
test.selectize.refreshOptions();
597+
expect($.isEmptyObject(test.selectize.renderCache['option'])).to.be.equal(false);
598+
expect($.isEmptyObject(test.selectize.renderCache['item'])).to.be.equal(false);
599+
test.selectize.clearCache('option');
600+
expect($.isEmptyObject(test.selectize.renderCache['option'])).to.be.equal(true);
601+
expect($.isEmptyObject(test.selectize.renderCache['item'])).to.be.equal(false);
602+
});
603+
});
604+
570605
});
571606

572607
})();

0 commit comments

Comments
 (0)