<commit_before>'use strict';

var Model = require('ampersand-model');
var React = require('react');
var view = require('./view');

module.exports = function(config) {
  if (config == null) {
    throw new Error('config parameter is required');
  }

  var ModelViewMixin = {
    propTypes: {
      model: React.PropTypes.instanceOf(Model)
    },

    // most options can be passed via config or props
    // props takes precedence
    getDefaultProps: function(){
      return {
        model: config.model
      };
    },

    componentWillMount: function() {
      this._initialize();
    },

    _initialize: function() {
      // collection view passing in model
      if (this.props.model != null) {
        this.model = this.props.model;
      // router is passing in params
      // TODO: allow config of id key
      } else if (this.props && this.props.id != null) {
        this.model = new config.model();
        this.model.id = this.props.id;

        // set id attrib to id - needed for fetching
        if (this.model.idAttribute !== undefined) {
          this.model[this.model.idAttribute] = this.props.id;
        }

        // TODO: error handling and event hooks
        this.model.fetch();
      }

      // no model specified in props or config
      if (!this.model) {
        throw new Error('modelView never got a model');
      }

      this.listenTo(this.model, 'change', this.tryUpdate);
    }
  };

  return view(config, [ModelViewMixin]);
};<commit_msg>experimental new router stuff for modelview<commit_after>'use strict';

var Model = require('ampersand-model');
var React = require('react');
var view = require('./view');

function constructModel(Model, id) {
  var model = new Model();
  model.id = id;

  // set id attrib to id - needed for fetching
  if (model.idAttribute !== undefined) {
    model[model.idAttribute] = id;
  }
  return model;
}

module.exports = function(config) {
  if (config == null) {
    throw new Error('config parameter is required');
  }

  var ModelViewMixin = {
    routeParamsKey: 'id',
    propTypes: {
      model: React.PropTypes.instanceOf(Model)
    },

    // most options can be passed via config or props
    // props takes precedence
    getDefaultProps: function(){
      return {};
    },

    componentWillMount: function() {
      this._initialize();
    },

    _initialize: function() {
      var routeParamId = this.getParams()[this.routeParamsKey];

      // collection view passing in model
      if (this.props.model != null) {
        this.model = this.props.model;
      // collection is passing in params
      // TODO: remove this if nobody uses it soon
      } else if (this.props && this.props.id != null) {
        this.model = constructModel(config.model, this.props.id);
        // TODO: error handling and event hooks
        this.model.fetch();
      } else if (routeParamId) {
        this.model = constructModel(config.model, routeParamId);
        // TODO: error handling and event hooks
        this.model.fetch();
      }

      // no model specified in props or config
      if (!this.model) {
        throw new Error('modelView never got a model');
      }

      this.listenTo(this.model, 'change', this.tryUpdate);
    }
  };

  return view(config, [ModelViewMixin]);
};
{'fraction_non_alphanumeric': 0.1258783990223037, 'fraction_numerical': 0.0, 'mean_word_length': 2.4175365344467643, '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': '2824530', 'n_tokens_mistral': 945, 'n_tokens_neox': 923, 'n_words': 276}