9951 explained code solutions for 126 technologies


backbone.jsHow do I use RequireJS with Backbone.js?


RequireJS is a JavaScript module loader that can be used to modularize code and manage dependencies. It can be used with Backbone.js to help structure code and manage dependencies between models, collections, views, and routers.

The following example shows how to use RequireJS with Backbone.js to define a model, collection, view, and router.

// models/User.js
define(function(require) {
  var Backbone = require('backbone');

  var User = Backbone.Model.extend({
    defaults: {
      name: '',
      email: ''
    }
  });

  return User;
});

// collections/Users.js
define(function(require) {
  var Backbone = require('backbone');
  var User = require('models/User');

  var Users = Backbone.Collection.extend({
    model: User
  });

  return Users;
});

// views/UserListView.js
define(function(require) {
  var Backbone = require('backbone');
  var Users = require('collections/Users');

  var UserListView = Backbone.View.extend({
    tagName: 'ul',

    render: function() {
      this.collection.each(function(user) {
        var userView = new UserView({ model: user });
        this.$el.append(userView.render().el);
      }, this);
      return this;
    }
  });

  return UserListView;
});

// routers/UserRouter.js
define(function(require) {
  var Backbone = require('backbone');
  var UserListView = require('views/UserListView');

  var UserRouter = Backbone.Router.extend({
    routes: {
      'users': 'showUsers'
    },

    showUsers: function() {
      var users = new Users();
      var userListView = new UserListView({ collection: users });
      $('#content').html(userListView.render().el);
    }
  });

  return UserRouter;
});

The code above defines four modules:

  • User model
  • Users collection
  • UserListView view
  • UserRouter router

Each module is defined using the define function provided by RequireJS. This function takes a function as its first argument and an array of dependencies as its second argument. The dependencies are loaded before the function is executed, allowing the code inside the function to use the dependencies.

For more information, see the RequireJS documentation and the Backbone.js documentation.

Edit this code on GitHub