简体   繁体   English

在node.js中生成JavaScript以便在客户端使用的方法

[英]Ways to generate javascript in node.js for use on the client side

I recently watched Dav Glass' talk on YUI and node , and the server-side rendering of widgets got me particularly interested. 我最近观看了Dav Glass在YUI和node上的演讲,并且小部件的服务器端渲染使我特别感兴趣。

I want to do something similar, while still being able to use javascript for the widgets on the client side. 我想做类似的事情,同时仍然可以在客户端的小部件上使用javascript。 The problem I am facing is how to run code associated with the widget (adding click events. etc) once the page is loaded. 我面临的问题是页面加载后如何运行与小部件关联的代码(添加点击事件等)。

One way of doing it could be to write an init function for each widget, then turn it into a string and put it in a script-tag along with the rendered html, but I don't think that's a very pretty solution. 一种实现方法是为每个小部件编写一个init函数,然后将其转换为字符串,并将其与呈现的html一起放入脚本标签中,但我认为这不是一个非常好的解决方案。

Can anyone think of a better solution? 谁能想到更好的解决方案?

Have you considered using Dojo? 您是否考虑过使用Dojo? It also has a great widget system and parses the widgets out of the html. 它还具有出色的窗口小部件系统,并从html中解析出窗口小部件。

Rolling your own widget system has a lot of complications that you will run into, while building on Dojo will give you a ton of great components to start with. 滚动自己的窗口小部件系统会遇到很多复杂的问题,而在Dojo上构建将为您提供大量出色的组件。

you can just set dojo djConfig="parseOnLoad:true" in the dojo script tag and dojo will parse your widgets for you. 您只需在dojo脚本标签中设置dojo djConfig =“ parseOnLoad:true”,dojo就会为您解析您的小部件。

You can also make your own widgets and do some custom init stuff in the widget postCreate method, which is like your init method you mention. 您还可以制作自己的小部件,并在小部件postCreate方法中执行一些自定义init事情,就像您提到的init方法一样。

See this page for more about Dojo widget methods: http://dojotoolkit.org/reference-guide/dijit/_Widget.html 有关Dojo小部件方法的更多信息,请参见此页面: http : //dojotoolkit.org/reference-guide/dijit/_Widget.html

I have built a lot of my own Dojo widgets and really like the framework. 我已经构建了很多自己的Dojo小部件,并且非常喜欢该框架。

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

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