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.
I want to do something similar, while still being able to use javascript for the widgets on the client side. The problem I am fa开发者_运维百科cing 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.
Can anyone think of a better solution?
Have you considered using Dojo? It also has a great widget system and parses the widgets out of the 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.
you can just set dojo djConfig="parseOnLoad:true" in the dojo script tag and dojo will parse your widgets for you.
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.
See this page for more about Dojo widget methods: http://dojotoolkit.org/reference-guide/dijit/_Widget.html
I have built a lot of my own Dojo widgets and really like the framework.
精彩评论