2010-10-27 56 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)) 
    });      
    } 
});