<commit_before>'use strict';

var Backbone = require('../vendor/index').Backbone;
var $ = require('../vendor/index').$;
var Model = require('./learning-resource.model');
var View = require('./learning-resource.view');

module.exports = Backbone.Controller.extend({

  routes: {
    'learning-resource/:': 'list',
    'learning-resource/:id': 'showLearningResource'
  },

  initialize: function(){
    this.options.container = this.options.container || 'body';
    this.model = new Model();
  },

  // list: function(){
  //   this.resourceList = new Model();
  //   this.resourceListView = new View();
  //   this.resourceList.fetch();
  //   $('body').html(this.resourceListView.render.el);
  // },

  showLearningResource: function(learningResourceId, cb){
    this.fetchModel(learningResourceId, function(err, model){
      console.log(model);
      var view;

      if (err){
        view = this.renderError();
      } else {
        if (this.view) this.view.remove();
        this.view = new View({model: model});
        view = this.renderView();
      }

      if (cb){
        cb(err, view);
      }

    }.bind(this));
  },

  fetchModel: function(learningResourceId, cb){
    // this.model = new Model(); //initialize model to allow property setting
    this.model.set({id: learningResourceId});

    this.model.fetch({
      success: function(model, response, options){
        //console.log(model);
        cb(null, model);
      },
      error: function(model, response, options){
        //console.error(response);
        cb(response, model);
      }
    });
  },

  renderToContainer: function(view){
    return $(this.options.container).html(view);
  },

  renderView: function(){
    this.renderToContainer(this.view.render().$el);
    return this.view;
  },

  renderError: function(){
    return this.renderToContainer(
      '<p>There was a problem rendering this learning resource.</p>');
  }

});
<commit_msg>refactors fetchModel into showLearningResource - adds an initializeModel instead of adding to initialization - removes callbacks for success and error on fetch<commit_after>'use strict';

var Backbone = require('../vendor/index').Backbone;
var $ = require('../vendor/index').$;
var Model = require('./learning-resource.model');
var View = require('./learning-resource.view');

module.exports = Backbone.Controller.extend({

  routes: {
    'learning-resource/:': 'list',
    'learning-resource/:id': 'showLearningResource'
  },

  initialize: function(){
    this.options.container = this.options.container || 'body';
  },

  initializeModel: function(attributes){
    this.model = new Model(attributes);
    this.view = new View({model: this.model});
  },

  showLearningResource: function(learningResourceId){
    var self = this;
    var view = this.view;

    if (this.view) this.view.destroy(); //do i need this?

    this.initializeModel({id: learningResourceId});

    this.model.fetch({
      success: function(){
        view = self.renderView();
      },
      error: function() {
        view = self.renderError();
      }
    });
  },

  renderToContainer: function(view){
    return $(this.options.container).html(view);
  },

  renderView: function(){
    this.renderToContainer(this.view.render().$el);
    return this.view;
  },

  renderError: function(){
    return this.renderToContainer(
      '<p>There was a problem rendering this learning resource.</p>');
  }

});

{'fraction_non_alphanumeric': 0.16140350877192983, 'fraction_numerical': 0.0, 'mean_word_length': 3.0921052631578947, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '677430', 'n_tokens_mistral': 1013, 'n_tokens_neox': 983, 'n_words': 216}