简体   繁体   English

关闭模板(大豆模板)通过 Javascript 和 jQuery 呈现的问题

[英]Issue with Closure Template (soy templates) rendering via Javascript & jQuery

I'm using javascript along with jQuery to render the closure templates (soy templates).我使用 javascript 和 jQuery 来呈现闭包模板(soy 模板)。 I'm follwing its hello world example.我正在关注它的hello world示例。
My jsfiddle here .我的 jsfiddle在这里

As described in the example , the following code works,示例中所述,以下代码有效,

document.write(soy.examples.simple.helloWorld());

& provides & 提供

Hello world!你好,世界!

But when used along with jQuery to dynamically insert the closure template's content, its always returns empty, nothing gets appended.但是当与 jQuery 一起使用动态插入闭包模板的内容时,它总是返回空,没有附加任何内容。 $('#withoutContent').append(soy.examples.simple.helloWorld());


Ironically,on further looking, the following jQuery works correctly具有讽刺意味的是,进一步观察,以下 jQuery 工作正常

$('#withContent').append(soy.examples.simple.helloWorld().content);


i'm facing this issue while upgrading from a very old version of closure templates.我在从非常旧版本的闭包模板升级时遇到了这个问题。 We have a lot of such templates, updating each one's usage in javascript to use .content is cumbersome & errorprone.我们有很多这样的模板,更新每个人在 javascript 中的用法以使用.content是麻烦且容易出错的。
Why isn't jQuery able to properly get the contents of the template??为什么 jQuery 不能正确获取模板的内容??

Why isn't jQuery able to properly get the contents of the template?为什么 jQuery 不能正确获取模板的内容?

A call to soy.examples.simple.helloWorld() returns this object:soy.examples.simple.helloWorld()调用返回此对象:

{
  constructor: function(){goog$soy$data$SanitizedContent.call(this)},
  content: "Hello world!",
  contentDir: null,
  contentKind: { ... },
  getContent: function(){return this.content},
  toString: function(){return this.content}
}

As you can see, it's not HTML, it has a couple of properties and methods - one of them is toString() , which returns the .content .如您所见,它不是 HTML,它有几个属性和方法——其中之一是toString() ,它返回.content

document.write() expects a string argument. document.write()需要一个字符串参数。 If you pass something else (for example the object above) then it will call .toString() for you automatically.如果你传递其他东西(例如上面的对象),那么它会自动为你调用.toString() This is why这就是为什么

document.write(soy.examples.simple.helloWorld());

works.作品。

jQuery's $.append() accepts all kinds of things - plain strings, single DOM nodes, DOM node lists, other jQuery objects, functions - and does the appropriate thing for each argument type. jQuery 的$.append()接受所有类型的东西——纯字符串、单个 DOM 节点、DOM 节点列表、其他 jQuery 对象、函数——并为每个参数类型做适当的事情。 But in exchange for this flexibility it won't simply convert its argument to string.但是为了换取这种灵活性,它不会简单地将其参数转换为字符串。 Some random object jQuery does not recognize will be discarded.一些 jQuery 无法识别的随机对象将被丢弃。 This is why这就是为什么

$('#withoutContent').append(soy.examples.simple.helloWorld());

does not work.不起作用。

updating each one's usage in javascript to use .content is cumbersome & errorprone.更新每个人在 javascript 中的用法以使用 .content 是麻烦且容易出错的。

If you can type $('...').append() in all of those locations, then you can type .content in all of those locations.如果您可以在所有这些位置键入$('...').append() ,那么您就可以在所有这些位置键入.content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM