superfeedr / indexeddb-backbonejs-adapter

An indexedDB adapter for Backbonejs
http://blog.superfeedr.com/indexeddb-backbonejs-adapter/
MIT License
248 stars 61 forks source link

Improve UMD flexibility and consistency #57

Closed andornaut closed 10 years ago

andornaut commented 10 years ago

Dependencies and exports are currently managed in two places:

    var Backbone, _;
    if(typeof exports !== 'undefined'){
        _ = require('underscore');
        Backbone = require('backbone');
    } else {
        _ = window._;
        Backbone = window.Backbone;
    }

and

    if(typeof exports == 'undefined'){
        Backbone.ajaxSync = Backbone.sync;
        Backbone.sync = sync;
    }
    else {
        exports.sync = sync;
        exports.debugLog = debugLog;
    }

The behavior of backbone-indexeddb varies in different environments as well: In an environment where exports exists, backbone-indexeddb will export a sync() function, otherwise it will mutate the Backbone object by adding a sync() function.

This suggests two improvements:

1) Enhance the UMD definition to support CJS-like environments (such as browserify), AMD and browser globals.

2) Always add a sync() function to Backbone regardless of the environment; and export and sync() function in CJS-like environments.

I'll submit a PR that implements these suggested changes soon. The PR will use a variation on this UMD pattern, which is similar to the method used in Backbone-layoutmanager