<commit_before>(function(){
  var dc       = window.dc;
  var $        = dc.$;
  var _        = dc._;
  var Backbone = dc.Backbone;

  var definition = dc.embed.definition;
  var data = dc.embed.data;
  var views = dc.embed.views;

  definition.Document = definition.Document || Backbone.Model.extend({
    initialize: function(attributes){
      this.notes = new definition.NoteSet();
      //this.resources = new definition.ResourceSet(); // doesn't exist yet.
      this.on('sync', this.updateCollections, this);
    },
    
    updateCollections: function() {
      this.notes.reset(this.get('annotations'));
    },
    
    imageUrl : function(pageNumber) {
      if (!this._imageUrl) {
        var resources = this.get('resources');
        var urlTemplate = resources['page']['image'];
        this._imageUrl = urlTemplate.replace('{size}', 'normal').replace('{page}', pageNumber);
      }
      return this._imageUrl;
    },
  }, {
    extractId: function(url){ return url.match(/(\d+[A-Za-z-]+).js(on)?$/)[1]; }
  });
  
  definition.DocumentSet = definition.DocumentSet || Backbone.Collection.extend({ 
    model: definition.Document 
  });
  
  definition.Note = Backbone.Model.extend({});
  definition.NoteSet = Backbone.Collection.extend({
    model: definition.Note
  });

  definition.PageView = definition.PageView || Backbone.View.extend({
    initialize: function(options) {
      this.options = options;
      this.listenTo(this.model, 'sync', this.render);
    },

    render: function() {
      this.$el.html(JST['page']({model: this.model, pageNumber: this.options.page }));
      this.cacheDomReferences();
      this.cacheNaturalDimensions();
    },

    cacheDomReferences: function() {
      this.$image = this.$('.DC-page-image');
    },

    cacheNaturalDimensions: function() {
      var unstyledImage = $(new Image());
      var view = this;
      unstyledImage.load(function(){
        view.dimensions = {
          height: this.height,
          width: this.width,
          aspectRatio: this.height / this.width
        };
      });
      unstyledImage.attr('src', this.model.imageUrl(this.options.page));
    },

    currentScale: function() {
      return this.$image.width() / this.dimensions.width;
    }
  });
})();<commit_msg>Promises for control flow readability.<commit_after>(function(){
  var dc       = window.dc;
  var $        = dc.$;
  var _        = dc._;
  var Backbone = dc.Backbone;

  var definition = dc.embed.definition;
  var data = dc.embed.data;
  var views = dc.embed.views;

  definition.Document = definition.Document || Backbone.Model.extend({
    initialize: function(attributes){
      this.notes = new definition.NoteSet();
      //this.resources = new definition.ResourceSet(); // doesn't exist yet.
      this.on('sync', this.updateCollections, this);
    },
    
    updateCollections: function() {
      this.notes.reset(this.get('annotations'));
    },
    
    imageUrl : function(pageNumber) {
      if (!this._imageUrl) {
        var resources = this.get('resources');
        var urlTemplate = resources['page']['image'];
        this._imageUrl = urlTemplate.replace('{size}', 'normal').replace('{page}', pageNumber);
      }
      return this._imageUrl;
    },
  }, {
    extractId: function(url){ return url.match(/(\d+[A-Za-z-]+).js(on)?$/)[1]; }
  });
  
  definition.DocumentSet = definition.DocumentSet || Backbone.Collection.extend({ 
    model: definition.Document 
  });
  
  definition.Note = Backbone.Model.extend({});
  definition.NoteSet = Backbone.Collection.extend({
    model: definition.Note
  });

  definition.PageView = definition.PageView || Backbone.View.extend({
    initialize: function(options) {
      this.options = options;
      this.listenTo(this.model, 'sync', this.render);
    },

    render: function() {
      this.$el.html(JST['page']({model: this.model, pageNumber: this.options.page }));
      this.cacheDomReferences();
      if (this.dimensions) {
        this.renderNotes();
      } else {
        this.cacheNaturalDimensions().then(_.bind(this.renderNotes, this));
      }
    },
    
    renderNotes: function(){
      var scale = this.currentScale();
      this.model.notes.each(function(n){ console.log(n.id); });
    },

    cacheDomReferences: function() {
      this.$image = this.$('.DC-page-image');
    },

    cacheNaturalDimensions: function() {
      var view = this;
      return new Promise(function(resolve, reject){
        var unstyledImage = $(new Image());
        unstyledImage.load(function(){
          view.dimensions = {
            height: this.height,
            width: this.width,
            aspectRatio: this.height / this.width
          };
          resolve();
        });
        unstyledImage.attr('src', view.model.imageUrl(view.options.page));
      });
    },

    currentScale: function() {
      return this.$image.width() / this.dimensions.width;
    }
  });
})();
{'fraction_non_alphanumeric': 0.1599918682659077, 'fraction_numerical': 0.0004065867046147591, 'mean_word_length': 2.647146034099333, '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': '7084072', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1447, 'n_words': 257}