Search code examples
javascriptbackbone.jsamdthorax.js

Clean method of sharing an object (or state) between modules with AMD and Backbone


I've got a Backbone/AMD app and what I would like to do is have a model/object be fetched in the main module when the app loads, then be either accessible to or be able to be loaded into subsequent modules without the overhead of re-fetching it (it's a permissions model and thus is required pretty much everywhere).

Is there a clean way of doing this?


Solution

  • Simply make it its own dependency:

    define(["backbone", "text!api/v1/user/permissions"],
           function(Backbone, permissionJSON) {
             return new Backbone.Model(JSON.parse(permissionJSON));
    });