<commit_before>/*globals pollUntil*/
define(
  ['collection-interactions', 'collection-datasuitcases', 'collection-forms', 'collection-pending', 'feature!data', 'feature!api', 'collection-stars', 'domReady', 'collection-form-records'],
  function (InteractionCollection, DataSuitcaseCollection, FormCollection, PendingCollection, Data, API, StarsCollection, domReady, FormRecordsCollection) {
    "use strict";
    var Application = Backbone.Model.extend({

      idAttribute: "_id",

      defaults: {
        _id: window.BMP.BIC.siteVars.answerSpace,
        loginStatus: false
      },

      datastore: function () {
        this.data = new Data(window.BMP.BIC.siteVars.answerSpace + '-AnswerSpace');
        return this;
      },

      collections: function () {
        var app = this;

        if (this.collections._promise) {
          // return a cached promise when possible
          return this.collections._promise;
        }

        this.collections._promise = new Promise(function (resolve, reject) {
          pollUntil(function () {
            // need to wait for the data layer to be configured RE: cordova
            return !!app.data;
          }, null, function () {
            // now data is safe to use, so we can get started
            app.interactions = app.interactions || new InteractionCollection();
            app.datasuitcases = app.datasuitcases || new DataSuitcaseCollection();
            app.forms = app.forms || new FormCollection();
            app.pending = app.pending || new PendingCollection();
            app.stars = app.stars || new StarsCollection();
            app.formRecords = app.formRecords || new FormRecordsCollection();

            Promise.all([
              app.interactions.datastore().load(),
              app.datasuitcases.datastore().load(),
              app.forms.datastore().load(),
              app.pending.datastore().load(),
              app.stars.datastore().load(),
              app.formRecords.datastore().load()
            ]).then(resolve, reject);
          });
        });

        return this.collections._promise;
      },

      setup: function () {
        var app = this;

        return new Promise(function (resolve, reject) {
          app.fetch({
            success: resolve,
            error: reject
          });
        });
      },

      populate: function () {
        var app = this;

        if (!(navigator.onLine || window.BMP.BIC.isBlinkGap)) {
          return Promise.resolve();
        }

        return Promise.resolve(API.getAnswerSpaceMap())
          .then(
            function (data) {
              return Promise.all(_.compact(_.map(data, function (value, key) {
                var model;
                if (key.substr(0, 1) === 'c' || key.substr(0, 1) === 'i') {
                  model = value.pertinent;
                  model._id = model.name.toLowerCase();
                  model.dbid = key;
                  app.interactions.add(model, {merge: true});
                  return model._id;
                }
                if (key.substr(0, 1) === 'a') {
                  return new Promise(function (resolve, reject) {
                    model = {
                      _id: window.BMP.BIC.siteVars.answerSpace.toLowerCase(),
                      dbid: key
                    };
                    app.interactions.add(model, {merge: true});
                    app.save(value.pertinent, {
                      success: function () {
                        resolve(window.BMP.BIC.siteVars.answerSpace.toLowerCase());
                      },<commit_msg>`BMP.BIC.populate()` now waits for collections to exist<commit_after>/*globals pollUntil*/
define(
  ['collection-interactions', 'collection-datasuitcases', 'collection-forms', 'collection-pending', 'feature!data', 'feature!api', 'collection-stars', 'domReady', 'collection-form-records'],
  function (InteractionCollection, DataSuitcaseCollection, FormCollection, PendingCollection, Data, API, StarsCollection, domReady, FormRecordsCollection) {
    "use strict";
    var Application = Backbone.Model.extend({

      idAttribute: "_id",

      defaults: {
        _id: window.BMP.BIC.siteVars.answerSpace,
        loginStatus: false
      },

      datastore: function () {
        this.data = new Data(window.BMP.BIC.siteVars.answerSpace + '-AnswerSpace');
        return this;
      },

      collections: function () {
        var app = this;

        if (this.collections._promise) {
          // return a cached promise when possible
          return this.collections._promise;
        }

        this.collections._promise = new Promise(function (resolve, reject) {
          pollUntil(function () {
            // need to wait for the data layer to be configured RE: cordova
            return !!app.data;
          }, null, function () {
            // now data is safe to use, so we can get started
            app.interactions = app.interactions || new InteractionCollection();
            app.datasuitcases = app.datasuitcases || new DataSuitcaseCollection();
            app.forms = app.forms || new FormCollection();
            app.pending = app.pending || new PendingCollection();
            app.stars = app.stars || new StarsCollection();
            app.formRecords = app.formRecords || new FormRecordsCollection();

            Promise.all([
              app.interactions.datastore().load(),
              app.datasuitcases.datastore().load(),
              app.forms.datastore().load(),
              app.pending.datastore().load(),
              app.stars.datastore().load(),
              app.formRecords.datastore().load()
            ]).then(resolve, reject);
          });
        });

        return this.collections._promise;
      },

      setup: function () {
        var app = this;

        return new Promise(function (resolve, reject) {
          app.fetch({
            success: resolve,
            error: reject
          });
        });
      },

      populate: function () {
        var app = this;

        if (!(navigator.onLine || BMP.BlinkGap.isHere())) {
          return Promise.resolve();
        }

        return app.collections()
          .then(function () {
            return Promise.resolve(API.getAnswerSpaceMap());
          })
          .then(
            function (data) {
              return Promise.all(_.compact(_.map(data, function (value, key) {
                var model;
                if (key.substr(0, 1) === 'c' || key.substr(0, 1) === 'i') {
                  model = value.pertinent;
                  model._id = model.name.toLowerCase();
                  model.dbid = key;
                  app.interactions.add(model, {merge: true});
                  return model._id;
                }
                if (key.substr(0, 1) === 'a') {
                  return new Promise(function (resolve, reject) {
                    model = {
                      _id: window.BMP.BIC.siteVars.answerSpace.toLowerCase(),
                      dbid: key
                    };
                    app.interactions.add(model, {merge: true});
                    app.save(value.pertinent, {
                      success: function () {
                        resolve(window.BMP.BIC.siteVars.answerSpace.toLowerCase());
                      },
{'fraction_non_alphanumeric': 0.12337217272104181, 'fraction_numerical': 0.0016449623029472242, 'mean_word_length': 1.7183308494783904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '95618', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1863, 'n_words': 411}