including existing dom-element with handlebars

给你一囗甜甜゛ 提交于 2019-12-13 17:42:29

问题


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 $el of 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!