Goofyahead
Goofyahead

Reputation: 5884

Loading templates with backbone js

I'm starting in javascript development, and did a simple project with node.js as a rest API and a client using backbone, everything look perfectly till I want to get my templates out of my js.

I found different approaches, some of them with some time (like one year old) but I can't understand which one could be better:

External template in Underscore

RequireJS: Loading modules including templates and CSS

Best way to asynchronously load underscore templates

http://api.jquery.com/jQuery.template/

It seems that require is the best approach, but maybe I'm missing something, I do wan't to make things as clean as possible since I'm in the learning/having fun phase :D

Any good article or github project with a good structure or any light on this will be appreciated.

Thanks.

Excuse any major spelling mistake, not an English speaker :)

--EDIT-- found some interesting videos to understand how to start and wrap things with require.js http://www.youtube.com/watch?v=VGlDR1QiV3A

http://www.youtube.com/watch?v=M-wjQjsryMY

Upvotes: 11

Views: 10789

Answers (2)

user1936059
user1936059

Reputation:

I would recommend using require.js with text plugin. Mixing html templates as strings in javascript variable is bad idea, as well as using something like <script type="text/template"></script>.

Here is one very good series on backbone.js which covers template loading and project build as well: http://dailyjs.com/2012/11/29/backbone-tutorial-1/. Github project is also provided there.

Upvotes: 2

hunterloftis
hunterloftis

Reputation: 13799

Require is a good option from the ones you listed.

Is there a reason you haven't considered simply:

  1. Storing templates in the pages that use them as <script type='text/template'> nodes?

  2. Storing templates as text (non-JS) files and loading them via XHR on pages that use them?

Upvotes: 2

Related Questions