I've been playing with the beta jQuery Templating engine. I started to play with jQuery Mobile over the weekend. Basically, I wanted to display information from a web service, put it into a ul/li list and have it display information. I could call the web service and get data back. Everything was working just fine in that I could get the data back. Unfortunately, when I bound the data, it just looked like a ul/li list. I knew I need to do something, but danged if I could figure out what I was doing wrong. Thankfully, my buddy Dave Ward pointed out what the problem was. I needed to refresh my listview. Thanks to Dave, here's the code that I came up with: The one piece that I was missing was the listview's refresh method.
<ul id="OptionContainer" data-role="listview" data-theme="a">
</ul>
<script id="userTemplate" type="text/x-jQuery-tmpl">
<li class="ul-li-icon">
<a href="">
<img src="@Href("~")images/users.png" height="30px" />
${UserName} ${FirstName} ${MiddleName} ${LastName}
</a>
</li>
</script>
<script language="javascript" type="text/javascript">
jQuery(function ($) {
$.ajax({
type: "POST",
url: "@Href("~")Services/Users.cshtml",
dataType: "json",
contentType: "application/json",
success: function (outPut) {
Users(outPut);
},
error: function (xml, err) {
alert(xml.responseText);
//for(m in xml)
// alert(m);
//alert("err:" + err);
}
});
});
function Users(result){
$("#userTemplate").tmpl(result).appendTo("#OptionContainer");
$("#OptionContainer").listview("refresh");
}