Clear jQuery template cache

1,300 views
Skip to first unread message

David Wengier

unread,
Jun 15, 2011, 7:35:19 PM6/15/11
to KnockoutJS
Does anyone have any nice methods for clearing the jQuery template
cache? It says on the knockout page that this "may be considered" if a
good reason can be shown.. well I have one!

I have a page that dynamically pops up an MVC PartialView. This
PartialView then calls another PartialView that contains my template.
I am utilising the MVC view engine to find the correct view, and
therefore template, depending on the current controller context, and
its working well from that point of view: When I ask for the "summary"
template, it finds the right one.

The problem is that if I open a page from Controller A, then jQuery
templates its "summary" template, so that when I later open a page
from Controller B, whilst MVC finds the right file and renders the
right template into the DOM, jQuery still tries to render the old one,
which then doesn't work because the ViewModel is different.

Any advice would be great. I don't really want to have to suffix my
template names with the current Controller name just to get around
this, as that would be an annoying convention to maintain.

rpn

unread,
Jun 15, 2011, 8:20:55 PM6/15/11
to knock...@googlegroups.com
You can do: delete $.template["yourTemplateName"]to clear the cached version..

Antonello

unread,
Jun 15, 2011, 9:00:47 PM6/15/11
to KnockoutJS
Uhmmm..... why don't you try jQXB Expression Binder ?

rpn

unread,
Jun 15, 2011, 9:56:48 PM6/15/11
to knock...@googlegroups.com
Hi Antonello-
Can you clarify your comment?  It looks like jQXB/M Expression Binder is a new library that is similar to Knockout.  Are you suggesting that David use it instead of Knockout or is there some way that it specifically help with his scenario?  

Thanks- Ryan

Kun Attila

unread,
Jun 22, 2011, 4:24:19 AM6/22/11
to KnockoutJS
I'm afraid this solution won't work since in my experience Knockout.js
directly modifies the HTML of the template script element for caching
purposes. A questionable practice indeed.

Mark Bradley

unread,
Jun 22, 2011, 5:26:19 AM6/22/11
to knock...@googlegroups.com
You could add the same template using this method:
https://github.com/SteveSanderson/knockout/blob/gh-pages/examples/resources/knockout.simpleGrid.js#L46

unfortunately you would have to do this before hand:
$("#templateId").remove();

On Wed, Jun 22, 2011 at 6:24 PM, Kun Attila <kun.att...@gmail.com> wrote:
> I'm afraid this solution won't work since in my experience Knockout.js
> directly modifies the HTML of the template script element for caching
> purposes. A questionable practice indeed.

This is an example of manual template management:
https://github.com/SteveSanderson/knockout/blob/gh-pages/examples/resources/knockout.simpleGrid.js#L46

unfortunately all that does is doa document.write, which is not very cool.
https://github.com/SteveSanderson/knockout/blob/master/src/templating/jquery.tmpl/jqueryTmplTemplateEngine.js#L99

you would need to do this before replacing an added template:
$("#templateid).remove();
delete $.template["templateid"];

>
> On Jun 16, 2:20 am, rpn <rnieme...@gmail.com> wrote:
>> You can do: delete $.template["yourTemplateName"]; to clear the cached
>> version..

--
-barkmadley
sent from an internet enabled device

rpn

unread,
Jun 22, 2011, 9:15:06 AM6/22/11
to knock...@googlegroups.com
Hi Kun-
Can you clarify which part does not work?  My impression was that the original poster had a template loaded, then it gets replaced by a different template, but the jQuery template plug-in still has it cached.  

This call will remove it from the cache: delete $.template["yourTemplateName"];


So, in this case it is not important what is cached, just that it is cached.

Mark Bradley

unread,
Jun 22, 2011, 10:43:14 AM6/22/11
to knock...@googlegroups.com
I think this might be a case which you are missing.

http://jsfiddle.net/Rg5yV/5/

--

rpn

unread,
Jun 22, 2011, 11:07:50 AM6/22/11
to knock...@googlegroups.com
I guess that makes sense, if one was really going to just manipulate the current text in the script tag.  It doesn't seem too realistic to me though.

I think that this seems closer to the original question: http://jsfiddle.net/rniemeyer/Rg5yV/ where the entire element goes away and is replaced by a new element. In that case the isRewritten flag would be gone.

Antonello

unread,
Jul 8, 2011, 11:19:33 AM7/8/11
to KnockoutJS
Hi,
jQXB simplify data binding and rendering data on page. It uses jQuery
as engine for traversing and manipulating DOM and to interact with
server. With jQuery and jQXB i can do "less less code and do more and
more" :-)
Reply all
Reply to author
Forward
0 new messages