<commit_before>/* # marionette.enhancdController

Brought to you by [Use All Five, Inc.](http://www.useallfive.com)

```
Author: <NAME> <<EMAIL>>
Author URI: [http://www.useallfive.com](http://www.useallfive.com)
Repository: https://github.com/UseAllFive/marionette.babyBird
```

Add `.show` method to a `Marionette.Controller`. Requires that `region` is
passed in as an option at instantiation.

Inspired by [Backbone Rails, Loading Views](http://www.backbonerails.com/screencasts/loading-views).

*/
(function(factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['marionette', 'backbone', 'jquery', 'underscore', 'spin', 'jquery.spin'], factory);
    } else {
        // Use browser globals. Will fail if they are not yet loaded.
        /*globals Marionette, Backbone, $, _, spin */
        factory(Marionette, Backbone, $, _, spin);
    }
}(function(Marionette, Backbone, $, _) {
    var LoadingController;
    var LoadingView;
    var appControllerLoaded = false;
    var controllerRegistry = {};
    var originalFunctions;
    var showLoading;
    var whenFetched;

    whenFetched = function(entities, callback) {
        var xhrs;

        xhrs = _.chain([entities]).flatten().pluck('_fetch').value();

        $.when.apply(this, xhrs).done(callback);
    };

    // Save a copy of the original functions.
    originalFunctions = {
        applicationConstructor: Marionette.Application.prototype.constructor,
        controllerConstructor: Marionette.Controller.prototype.constructor,
        destroy: Marionette.Controller.prototype.destroy
    };

    // Override Backbone.sync
    overrideSync();

    function overrideSync() {
        var _sync = Backbone.sync;

        Backbone.sync = function(method, entity, options) {
            var sync;

            if (_.isUndefined(options) || options === null) {
                options = {};
            }

            sync = _sync(method, entity, options);
            if (!entity._fetch && method === "read") {
                entity._fetch = sync;
            }

            return sync;
        };
    }

    // Static function to create a `close` or `destroy` function on a
    // `controllerInstance`.
    function overrideDestroyOrCloseFunction(controllerInstance) {
        var functionName;
        var originalFunction;

        // Determine if we have a `close` or a `destroy` function. The API
        // changed going to Backbone 2.
        if (_.isFunction(controllerInstance.close)) {
            // < Backbone 2
            functionName = 'close';
        } else if (_.isFunction(controllerInstance.destroy)) {
            // >= Backbone 2
            functionName = 'destroy';
        } else {
            throw new Error('Could not find a close or destroy method on the controller');
        }

        // Save the original function.
        originalFunction = controllerInstance[functionName];

        // Modify the original function.
        controllerInstance[functionName] = function() {
            delete this.region;
            delete this.options;

            // Remove instance from the registry.<commit_msg>Cleanup to UA5 Frontend Standards<commit_after>/* # marionette.enhancdController

Brought to you by [Use All Five, Inc.](http://www.useallfive.com)

```
Author: <NAME> <<EMAIL>>
Author URI: [http://www.useallfive.com](http://www.useallfive.com)
Repository: https://github.com/UseAllFive/marionette.babyBird
```

Add `.show` method to a `Marionette.Controller`. Requires that `region` is
passed in as an option at instantiation.

Inspired by [Backbone Rails, Loading Views](http://www.backbonerails.com/screencasts/loading-views).

*/
(function(factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['marionette', 'backbone', 'jquery', 'underscore', 'spin', 'jquery.spin'], factory);
    } else {
        // Use browser globals. Will fail if they are not yet loaded.
        /*globals Marionette, Backbone, $, _, spin */
        factory(Marionette, Backbone, $, _, spin);
    }
}(function(Marionette, Backbone, $, _) {
    var LoadingController;
    var LoadingView;
    var appControllerLoaded = false;
    var controllerRegistry = {};
    var originalFunctions;
    var showLoading;
    var whenFetched;

    whenFetched = function(entities, callback) {
        var xhrs;

        xhrs = _.chain([entities]).flatten().pluck('_fetch').value();

        $.when.apply(this, xhrs).done(callback);
    };

    // Save a copy of the original functions.
    originalFunctions = {
        applicationConstructor: Marionette.Application.prototype.constructor,
        controllerConstructor: Marionette.Controller.prototype.constructor,
        destroy: Marionette.Controller.prototype.destroy
    };

    function overrideSync() {
        var _sync = Backbone.sync;

        Backbone.sync = function(method, entity, options) {
            var sync;

            if (_.isUndefined(options) || options === null) {
                options = {};
            }

            sync = _sync(method, entity, options);
            if (!entity._fetch && method === 'read') {
                entity._fetch = sync;
            }

            return sync;
        };
    }

    // Override Backbone.sync
    overrideSync();

    // Static function to create a `close` or `destroy` function on a
    // `controllerInstance`.
    function overrideDestroyOrCloseFunction(controllerInstance) {
        var functionName;
        var originalFunction;

        // Determine if we have a `close` or a `destroy` function. The API
        // changed going to Backbone 2.
        if (_.isFunction(controllerInstance.close)) {
            // < Backbone 2
            functionName = 'close';
        } else if (_.isFunction(controllerInstance.destroy)) {
            // >= Backbone 2
            functionName = 'destroy';
        } else {
            throw new Error('Could not find a close or destroy method on the controller');
        }

        // Save the original function.
        originalFunction = controllerInstance[functionName];

        // Modify the original function.
        controllerInstance[functionName] = function() {
            delete this.region;
            delete this.options;

            // Remove instance from the registry.
{'fraction_non_alphanumeric': 0.11714824120603015, 'fraction_numerical': 0.0010992462311557789, 'mean_word_length': 2.3895689196381054, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1195625', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1677, 'n_words': 469}