Using the jquery .data() it is easy to attach an object to an html element.
Is this also possible in jsRender?
I render a list of divs and would like to attach the data object to each div, so when a user clicks on it, it is available for me.
You may try "data-your-value" in the markup so it is also rendered in HTML.
When user clicks it, calls the data() method to find the data.
OK, but how do I attach an object to it? As a json string? Could be a large string.
Is it possible during the render to create an dictionary and push the object in it with the template index as key?
Like this:
<script id="tmplX" type="text/x-jsrender">
<div data-id="{{:#index}}">
<!-- Here some code to push #data in a dictionary using #index as key -->
{{:SomethingToShow}}
</div>
</script>
Now I can retreive the object from the dictionary using the data-id attribute.
But how can I push the data to the array / dictionary in the template?
Pexxx
Member
1 Points
8 Posts
Attach an object in jsRender
Aug 28, 2012 02:06 PM|LINK
Hi,
Using the jquery .data() it is easy to attach an object to an html element.
Is this also possible in jsRender?
I render a list of divs and would like to attach the data object to each div, so when a user clicks on it, it is available for me.
Thanks, Perry
BU XI - MSFT
All-Star
22367 Points
2704 Posts
Microsoft
Re: Attach an object in jsRender
Aug 30, 2012 04:09 AM|LINK
Hello
As jQuery now implements use data-your-value to store data, you can try writing them in the template, see if it works,
http://api.jquery.com/data/
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Pexxx
Member
1 Points
8 Posts
Re: Attach an object in jsRender
Aug 30, 2012 06:40 AM|LINK
Ik know the $.data() function, but how can I use this in a jsRender template iteration over a collection of objects?
BU XI - MSFT
All-Star
22367 Points
2704 Posts
Microsoft
Re: Attach an object in jsRender
Aug 31, 2012 03:42 AM|LINK
You may try "data-your-value" in the markup so it is also rendered in HTML. For example,
<script id="myTemplate" type="text/x-jsrender ">
<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>
</script>
When user clicks it, calls the data() method to find the data.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Pexxx
Member
1 Points
8 Posts
Re: Attach an object in jsRender
Aug 31, 2012 06:52 AM|LINK
OK, but how do I attach an object to it? As a json string? Could be a large string.
Is it possible during the render to create an dictionary and push the object in it with the template index as key?
Like this:
<script id="tmplX" type="text/x-jsrender"> <div data-id="{{:#index}}"> <!-- Here some code to push #data in a dictionary using #index as key --> {{:SomethingToShow}} </div> </script>Now I can retreive the object from the dictionary using the data-id attribute.
But how can I push the data to the array / dictionary in the template?