I made a simple app using backbone.js and require.js. Earlier i used to have just one index.html file and used to dynamically render/hide different views. Now with require.js, i still have index.html file but i have created separate html files for each of my four views in the app, and i put them all in templates folder. Main point is, these four html files don’t have the <!DOCTYPE html></html> tags, just the <div> tags for the view.
I’m not sure this is the right way to do it using require.js. Should i integrate all html code into just one index.html and using <script> tags for templating?
You shouldn’t put your templates into one big html file,
require.jsandBackbone.jsare the perfect combination to have everything in highly flexible modules, loaded only when neccessary.With only a few modules you may not notice their advantages, but trust me, if you write more complex, dynamically growing high speed web applications, you save yourself hours of debugging and refactoring, and your code will be very simple to read and modify.
You have several ways to handle templates with Backbone, e.x.
this.$el.html( _.template(template, this.model.toJSON() ))if you loaded your template into atemplatevariable.It won’t affect speed, templates are only a few kilobytes. Comparing to the fact that your page is likely to already load a dozen files(many icons, a few images, css-es, js-es) even without
BB.jsorRequire.jsand modules, a new few-kilobyte-big file will not be noticable. Also, you can cache templates after first load if you useRequire.jsto load them.