Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Backbone.js Patterns and Best Practices
  • Table Of Contents Toc
Backbone.js Patterns and Best Practices

Backbone.js Patterns and Best Practices

By : Swarnendu De
4.3 (9)
close
close
Backbone.js Patterns and Best Practices

Backbone.js Patterns and Best Practices

4.3 (9)
By: Swarnendu De

Overview of this book

Table of Contents (19 chapters)
close
close
Backbone.js Patterns and Best Practices
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
2
Precompiling Templates on the Server Side
4
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.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Backbone.js Patterns and Best Practices
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon