locked
Bug in Template cache when using href to load templates,

    General discussion

  • Hello all,

       I have found an issue with using Templates that are loaded from hrefs.. The templates seem to aggressively cache results rather than re-computing for each render.  I built a sample project to show what I mean, when you run it, you will see that templates that are inline to the HTML can be loaded any which way and work correctly, but as soon as a template is loaded from an href instead of the DOM it starts acting wierd (the wierd part is, the value its showing is the LAST value rendered not the first..

    https://skydrive.live.com/redir.aspx?cid=4fbacde1825fb530&resid=4FBACDE1825FB530!209&parid=4FBACDE1825FB530!208&authkey=!AORciabF16fn2zA

    Anyways, I am 99% sure this is a bug in WinJS.Binding.Template so I figured I would share... If I find a short term workaround I'll post it here too.

    Josh

    Wednesday, December 28, 2011 3:30 PM

All replies

  • So, short term workaround (for those of us that want to reuse our templates between pages)...

    your template doesn't have to be in the page to work right, it just has to be a DOM element you pass to create your template. So it just takes a few steps that you can convert into a useful utility method with a little bit of effort ( I'll write a blog post about it sometime this week).  But till then here are the steps.

    1) load your HTML fragment via xhr (could use the Fragment API but I don't quite understand it yet so I am skipping it for now).

    2) convert your string to an element (document.createElement('holderTag'), then holderTag.innerHTML = yourFragment);

    3) query your holderTag via WinJS.Utilities.query().. (you will want to just grab the first item in the array because you can't pass an array to new WinJS.Binding.Template)

    4) pass your found element ( new WinJS.Binding.Template(templateElement))

    now you have a dynamicly loaded template that isn't broken.

    Josh

    Wednesday, December 28, 2011 5:09 PM