包括现有的带把手的DOM元素 [英] including existing dom-element with handlebars

查看:94
本文介绍了包括现有的带把手的DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过Handlebars包含现有的dom元素,同时能够保留对所述dom元素的引用?

解释:


  • 我有一个jQuery元素 $ el ,其中我想在句柄模板中包含dom元素( $ el [0] )。

  • 在handlebars包含模板之后,我有一些使用 $ el.html(new stuff)的jquery代码(这个模板包含 $ el [0]



通常的解决方案是重写代码提供一个选择器,以便jquery可以访问该元素但是,需要更改 $ el 的代码不知道模板中的位置 $ el [ 0] 将被使用,因为这是可配置的。必须通过config指定选择符显然是可能的,但这对我来说并不真正感到枯燥。


$ b $所以,任何方式来做到这一点?

解决方案

我实现这个与一个handlebars助手注入一个唯一的ID和后期渲染保持了uniqueid - >元素的绑定。



在postrender上,我只需找到具有unique-id的元素,并将每个元素更新为映射的el来绑定。


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?

解决方案

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.

这篇关于包括现有的带把手的DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆