Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Backbone.js Patterns and Best Practices

You're reading from   Backbone.js Patterns and Best Practices Improve your Backbone.js skills with this step-by-step guide to patterns and best practice. It will help you reduce boilerplate in your code and provide plenty of open source plugin solutions to common problems along the way.

Arrow left icon
Product type Paperback
Published in Jan 2014
Publisher Packt
ISBN-13 9781783283576
Length 174 pages
Edition Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Swarnendu De Swarnendu De
Author Profile Icon Swarnendu De
Swarnendu De
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Backbone.js Patterns and Best Practices
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. Reducing Boilerplate with Plugin Development FREE CHAPTER 2. Working with Views 3. Working with Models 4. Working with Collections 5. Routing Best Practices and Subrouting 6. Working with Events, Sync, and Storage 7. Organizing Backbone Applications – Structure, Optimize, and Deploy 8. Unit Test, Stub, Spy, and Mock Your App Books, Tutorials, and References Precompiling Templates on the Server Side
Organizing Templates with AMD and Require.js Index

Appendix C. Organizing Templates with AMD and Require.js

Asynchronous Module Definition (AMD) is a JavaScript API used to define modules and load module dependencies asynchronously. It is a fairly new yet very robust concept that many developers are adopting nowadays. In Chapter 7, Organizing Backbone Applications – Structure, Optimize, and Deploy, we covered AMD with Require.js in detail. If you need more details on this library, we recommend you visit http://requirejs.org/ to get a complete overview and then come back to this section.

In general, Require.js treats every file's content as JavaScript. So, we cannot load our template files, if they aren't JavaScript files, in the same manner as JavaScript files. Fortunately for templates, there is a text plugin that allows us to load text-based dependencies. Any file that we load using this file will be treated as a text file and the content that we receive will be a string; it can be used easily with your template methods. To use this plugin, just prepend text! to the file path and the file contents will be retrieved as plain text; to do this, follow this example:

// AMD Module definition with dependencies
define([
    'backbone',
    'underscore',

    // text plugin that gets any file content as text
    'text!../templates/userLogin.html'
  ],
  function (Backbone, _, userLoginTpl) {
    'use strict';

    var UserLogin = Backbone.View.extend({
      // Compile the template string that we received 
      template: _.template(userLoginTpl),
      render: function () {
        this.$el.html(this.template({
          username: 'johndoe',
          password: 'john'
        }));

        return this;
      }
    });

    return UserLogin;
  });

The benefit of using this mechanism is that you can organize your templates by creating separate template files and they are automatically included in your modules. Since this involves asynchronous loading, the files are downloaded via AJAX requests, something we already decided as being a bad idea. However, Require.js comes with an r.js optimization tool that builds the modules and can save these extra AJAX requests by inlining these templates with their respective modules.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image