开发者

Nested AJAX calls in jQuery Templates

I have a jQuery Template that is rendering a list of comments:

<script id="CommentTemplate" type="text/x-jquery-tmpl">
    <开发者_开发问答;div class="Entry" id="${ID}">${Comment}</div>
</script>

This is being populated by a AJAX call and is working fine. I want to add a list of replies asynchronously. I would like to trap the event when this template content is rendered and append the replied to the Entry. Is there a way to call a JS function, say ShowResults(id) populated by ${ID} when each Entry is rendered?

Thanks


So after some digging I found a pretty elegant solution. You can pass an set of key value pairs to the template via the options parameter in the tmpl signature. I pass a function call as shown below:

$("#CommentTemplate").tmpl(result, {
    LoadReplies: function (key) {
        GetReplies(key);
    }
}).appendTo("#CommentsListing");

Then in the GetReplies I do an AJAX call and apply to a second template. Worked like a charm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜