BorisMoore / jquery-tmpl

The original official jQuery Templates plugin. This project was maintained by the jQuery team as an official jQuery plugin. It is no longer in active development, and has been superseded by JsRender.
3.23k stars 1.01k forks source link

bound events lost on tmplItem.update() #86

Open BorisMoore opened 13 years ago

BorisMoore commented 13 years ago

Copied from https://github.com/nje/jquery-tmpl/issues#issue/33:

Created 3 months ago by ethermal if I have a template then I bind a blur event to it then later call the tmplItem.update() function the bound events are lost. this is overridden by using .live instead of .bind. But during an update() is it necessary to destroy & recreate the objects instead of just updating the values?

ps. I noticed a bit of odd behaviour and perhaps I should create another issue for this. If i call tmplItem.update() from within a event handler I cannot set focus back to element. So for example in my template create a table of text fields, I then bind keypress and blur events then as a user tabs their way through the form on each blur I want to recalc totals and taxes which means I need to call tmplItem.update(). After I do that i can't set focus back to the original item or to any item for that matter.

example:

$.tmpl('input type=text class="ne" value="${data}" ', {data:50}); $(.ne').live('blur', function() { this.templItem().update();}); $(.ne').live('keypress',      function(e) { if (e.keyCode==13) this.trigger('blur').focus();} //the focus here does not work.

Comments ethermal December 08, 2010 to work around this in the keypress event I had to do this

var el = $(this)[0].id;
this.trigger('blur');
setTimeout(function() { $('#'+el).focus();}, 0);

ethermal December 08, 2010 even timeout wasn't necessary

aaronshaf commented 13 years ago

+1 :)

aaronshaf commented 13 years ago

I have multiple events bound to an element using live. The first event runs tmplItem.update(), but all other events are then lost. None of the subsequent bound events run on the element.

aaronshaf commented 13 years ago

My fix was to trigger an "afterUpdate" event from within the update event which updated the template.

BorisMoore commented 13 years ago

It is better to use delegate. But even using live can work fine. You can also call item.update(), and then get the item.nodes and rebind. Or you can use the rendered event.

See http://jsbin.com/ukefe3.

You could put a small test case on jsbin or jsfiddle showing the case of the live event not getting triggered. It does work in my example above...

rdworth commented 13 years ago

Thanks for taking the time to submit this issue. Just wanted to let you know this plugin is no longer being actively developed or maintained by the jQuery team. See README for more info.