A third option for using jQuery templates
Dave Ward has a great post about defining jQuery templates. There's a third method that he doesn't mention in his post. The "embedd-and-grab/clone" method. I've used this method before for simple element cloning of templates.
We can create a div, or really any element you want, to hold our templates. What does this gain us? Well if we are using a design tool, we can see what the template will look like before we have to render it. That may make it easier for a designer on your project. We don't have to make an AJAX call out to retrieve the external template, although Dave talks about how this really isn't an issue if you have caching set up correctly on your server. And frankly, for the amount of bytes that are in a typical template I can't imagine any successful AJAX request taking very long.
To use them, you simply grab the templates div and detach it from the DOM. If you assign the detached elements to a var, you can just use jQuery selectors to find the one you want to use. Because, remember most of the jQuery methods return the jQuery object itself.
-
var templates = $("#templates").detach();
-
$.tmpl(templates.find("#hello").text(), person);
Why do you want to use the detach method rather than the remove method? The detach method removes the elements from the DOM but keeps any jQuery data associated with them intact. Meaning you can use the $.data() method to add data to your templates and access the data before you compile your templates.
Dave Ward points out that I need to use "tempates.find()" rather than the default selector method on the jQuery object. Noted and updated
Twitter Updates for 2010-08-22
- [posted] Twitter Updates for 2010-08-21 http://bit.ly/bOpDka #
Powered by Twitter Tools
Twitter Updates for 2010-08-21
- [posted] Twitter Updates for 2010-08-20 http://bit.ly/du9klD #
- #herdingCode Herding Code 91: Listener-Powered Lightning Round: This week on Herding Code, K Scott, Jon, Kevin, an... http://bit.ly/d1WaI4 #
- Oslo: because SQL DDL is too hard I guess. #
Powered by Twitter Tools
Twitter Updates for 2010-08-20
- Microsoft to Co-Chair New W3C Web Performance Working Group http://bit.ly/dgOfXX #
- Seriously, do people actually conduct financial transactions on E*Trade? The website sucks and doesn't work 1/2 the time. #
- In unrelated news, I hate everything. #
- .@SaraJChipps JavaScript - The Good Parts by Douglas Crockford #BasicsTrumpsFrameworks #
- .@bmsullivan Right, but if you don't understand scope and events you might wright your handlers incorrectly and cause more problems. #
- #herdingCode Herding Code 91: Listener-Powered Lightning Round: This week on Herding Code, K Scott, Jon, Kevin, an... http://bit.ly/bU1jd7 #
- http://bit.ly/bU1jd7 where we finally tackle the burning question "Cake or pie?" #
- Did I just see a JavaFX article on DrDobbs? Seriously? Is JavaFX still around? Has anything other than examples been written using it? #
- For those wondering, yes Dr Dobbs is still around. At least online. http://bit.ly/9MH0Nf #
- I'm not registering for PDC10, but I will register for PDC10 afterparties.
# - “@dorianmuthig: Lala
” got me on my knees # - Cool, husband of one of my HS classmates is the President of SendThisFile.com http://j.mp/dxPYDH #fb #
- .@robconery Not so fast Conery! You don't speak for me! in reply to robconery #
- [posted] Twitter Updates for 2010-08-19 http://bit.ly/dbUgku #
- 5 assumptions MongoDB makes that users often miss. http://j.mp/9gGqBG #
- I have 4687 fake followers. Wait, that can't be right. #
Powered by Twitter Tools
Twitter Updates for 2010-08-19
- Ruby 1.9.2 is released http://bit.ly/aLHlEW #
- Free F# stand alone IDE (VS Shell) http://bit.ly/9zPBp5 #
- Interesting, If you install enough extensions in Chrome, you lose the default menus? Can't get to the "home" menu anymore. #
- .@jongalloway @OdeToCode he also loads jQuery twice for some reason. (fo' reals) #
- I am the terror that flaps in the night. I am the 8 am conference call that starts late... #fb #
- One thing I'd like is to be able to hook my iPhone up to a monitor for gaming/movies and use the iPhone as the controller. #
- That might require a beefier chip though and drain the battery faster. #
- I've noticed that multiple monitors aren't necessary if I can use Expose on osx or with the taskbar peaks in windows7 #
- /s/peaks/peeks sheesh #
- I don't get it? Why roll out a @lightswitch beta to MSDN subscribers? They aren't the target audience. #
Powered by Twitter Tools
The hardest part of software development has nothing to do with code
People who complain about how much "more" code they have to write in an MVC project versus a Webforms project, or really any project, prove to me that they have absolutely no idea where the REAL work is in ANY development project.
The main work in any software development project is FIGURING OUT WHAT TO BUILD. How you build it is trivial compared to the amount of time and effort you SHOULD put into discovering the users needs and working with them to solve their problems and make their life better.
Remember, that's the number one purpose for any piece of computer hardware or software. This cannot be stressed and repeated enough.
COMPUTERS SHOULD MAKE OUR LIVES BETTER!
How do we write programs that make lives better? By writing programs that fulfill their needs and ease the pain of their work. We still aren't at a point where we have a common, easy to understand vocabulary when it comes to build software. We often get it wrong the first, second, and third times. That's where the discipline and engineering comes into play.


