<commit_before>var SummaryModel = Backbone.Model.extend({
  state: 'ready', // ready | loading | errored
  text: '',
  summary: ''
});

var SummaryView = Backbone.View.extend({
  el: '#text-summary',
  bindings: {
  },

  events: {
    'change textarea': 'textareaChanged'
  },

  initialize: function() {
    console.log('view.initialize');
    this.textareaChanged();
    this.render();
  },

  render: function() {
    console.log('view.render');

    // TODO: Bind this view to model.state
    this.stateChanged(this.model.state);
  },

  textareaChanged: function() {
    // FIXME: Any more robust way to handle this?
    this.model.text = $('textarea[name=text]').val();
  },

  stateChanged: function(state) {
    console.log('stateChanged');
    if (state == 'ready') {
      $('#loading').hide();
      if (this.model.summary) {
        // TODO: Bind this view to model.summary
        $('#summary').html(this.model.summary).show();
      }
      else
        $('#summary').hide();
    }
    else if (state == 'loading') {
      $('#loading').show();
      $('#summary').hide();
    }
    else {
      throw 'Invalid state: ' + state;
    }
  }
});


$('form').on('submit', function(event) {
  event.preventDefault();

  var action = event.target.action;
  var text = window.summary.text;
  window.summary.state = 'loading';
  app.render();

  $.post(action, {text: text}, function(resp) {
    window.summary.summary = resp;
    window.summary.state = 'ready';
    app.render();
  }, 'text');
});


window.summary = new SummaryModel();
var app = new SummaryView({model: window.summary});
<commit_msg>Bind the view to the model.state<commit_after>var SummaryModel = Backbone.Model.extend({
  state: 'ready', // ready | loading | errored
  text: '',
  summary: ''
});

var SummaryView = Backbone.View.extend({
  el: '#text-summary',
  bindings: {
  },

  events: {
    'change textarea': 'textareaChanged',
    'change:state': 'stateChanged'
  },

  initialize: function() {
    console.log('view.initialize');
    this.textareaChanged();
  },

  render: function() {
    console.log('view.render');
  },

  textareaChanged: function() {
    // FIXME: Any more robust way to handle this?
    this.model.text = $('textarea[name=text]').val();
  },

  stateChanged: function(state) {
    console.log('stateChanged');
  }
});


$('form').on('submit', function(event) {
  event.preventDefault();

  var action = event.target.action;
  var text = model.text;
  model.set({state: 'loading'});

  $.post(action, {text: text}, function(resp) {
    model.set({summary: resp, state: 'ready'});
  }, 'text');
});


window.model = new SummaryModel();


// TODO: Could we remove this boiler-plate code?
model.on('change:summary', function(m, v) {
  console.log('model.summary changed');
});

model.on('change:state', function(m, state) {

  console.log('model.state changed:', state);

  // NOTE: At this point, the `model` sure looks like SummaryModel,
  // but it does not have `stateChanged` function defined inside the mode.
  // So we decided to do the work here until we figure what exactly is going
  // on.
  //
  // model.stateChanged(state);

  if (state == 'ready') {
    var summary = model.get('summary');
    $('#loading').hide();
    if (summary) {
      // TODO: Bind this view to model.summary
      $('#summary').html(summary).show();
    }
    else
      $('#summary').hide();
  }
  else if (state == 'loading') {
    $('#loading').show();
    $('#summary').hide();
  }
  else {
    throw 'Invalid state: ' + state;
  }
});

var app = new SummaryView({model: model});

$(function() {
  model.set({state: 'ready'});
});

{'fraction_non_alphanumeric': 0.18887665198237885, 'fraction_numerical': 0.0, 'mean_word_length': 3.142531356898518, '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': '3698075', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1143, 'n_words': 271}