问题
Is there a way to include an existing dom-element through Handlebars, while being able to keep a reference to said dom-element?
to explain:
- I have a jquery-element
$elof which I want to include the dom-element ($el[0]) in a handlebars template. - I have some jquery code that uses
$el.html("new stuff")after handlebars has included the template (again: this template contains$el[0]
The usual solution would be to rewrite the code by providing a selector so jquery can access the element. However, the code that needs to change $el doesn't know where in the template $el[0] will be used, since this is configurable. Having to specify the selector by config is possible obviously but this doesn't really feel dry to me.
So, any way to do this?
回答1:
I implemented this with a handlebars helper that injects a unique id and post-render keeps a ref from uniqueid -> element to bind.
On postrender I simply find the elements with the unique-ids and update each element to it's mapped el to bind.
来源:https://stackoverflow.com/questions/22046069/including-existing-dom-element-with-handlebars