开发者

Callback functions for jquery-templ?

开发者 https://www.devze.com 2023-01-21 08:15 出处:网络
I\'m using the plugin jquery-tmpl. Is there a way to specify a callback after a template is run? I want to do something like

I'm using the plugin jquery-tmpl. Is there a way to specify a callback after a template is run? I want to do something like

<script id='itemTemplate' type='text/html'>
  <li class="item" id=${timestampMs}>
    <span class="content">${content}</span> 
  </li>
  ${processItem($('#' + timestampMs))}
</script>

开发者_开发问答Where processItem does something to the <li> element that just got generated. As it's written, though, the element doesn't exist at the time processItem is called.

Here's how I run the template:

// Make the AJAX call to the service
$.ajax({
  dataType: "json",
  url: "/getItems",
  success: function(data) {
    // fill out template from json
    $('#itemTemplate').tmpl(data).appendTo('#container');
  }
});

Thanks!


After you call .tmpl, you have nodes that you can act upon, so you can do this:

$.ajax({
  dataType: "json",
  url: "/getItems",
  success: function(data) {
    // fill out template from json
    $('#itemTemplate').tmpl(data).each(function (index) {                           
      processItem($(this))
    }).appendTo('#container');               
  }
});

That's similar to your workaround, but you shouldn't need to reselect the items, and you should be able to chain the call.


Here's the workaround I'm using for now:

$.ajax({
  dataType: "json",
  url: "/getItems",
  success: function(data) {
    // fill out template from json
    $('#itemTemplate').tmpl(data).appendTo('#container');

    // now process the new events
    $('#container .item').each(function (index) {                           
      processItem($(this))
    });                     
  }
});
0

精彩评论

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