2010-10-27 58 views
1

我正在使用插件jquery-tmpl。有一种方法可以在模板运行后指定回调吗?我想这样做jquery-templ的回调函数?

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

processItem做一些事情到刚刚生成的<li>元素。因为它被写入,但是在调用processItem时元素不存在。

下面是如何运行模板:

// 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'); 
    } 
}); 

谢谢!

回答

4

你打电话.tmpl后,你有,你可以在行为节点,所以你可以这样做:

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

这与您的解决方法类似,但您不需要重新选择项目,并且应该可以将呼叫链接起来。

0

下面是我用现在的解决方法:

$.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)) 
    });      
    } 
});