<commit_before>/**
 * Base backbone view for vdom- see class declaration for more information
 *
 * @author    <NAME> <<EMAIL>>
 */
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var tungsten = require('../../src/tungsten');
var ViewWidget = require('./backbone_view_widget');

// Cached regex to split keys for `delegate`.
var delegateEventSplitter = /^(\S+)\s*(.*)$/;

/**
 * Provides generic reusable methods that child views can inherit from
 */
var BaseView = Backbone.View.extend({
  /*
   * Default to an empty hash
   */
  eventOptions: {},
  /**
   * Shared init logic
   */
  initialize: function(options) {
    if (!this.el) {
      return false;
    }
    this.options = options || {};

    // Pass router through options, setting router.view if not already set
    if (this.options.router) {
      this.router = this.options.router;
      this.router.view = this.router.view || this;
    }
    // Template object
    if (this.options.template) {
      this.compiledTemplate = this.options.template;
    }
    // VTree is passable as an option if we are transitioning in from a different view
    if (this.options.vtree) {
      this.vtree = this.options.vtree;
    }
    // First-pass rendering context
    if (this.options.context) {
      this.context = this.options.context;
    }
    // Handle to the parent view
    if (this.options.parentView) {
      this.parentView = this.options.parentView;
    }

    var dataItem = this.serialize();

    // Sanity check that compiledTemplate exists and has a toVdom method
    if (this.compiledTemplate && this.compiledTemplate.toVdom) {
      if (this.options.dynamicInitialize) {
        // If dynamicInitialize is set, empty this.el and replace it with the rendered template
        // @TODO set this.vtree to an empty node and just call render?
        while (this.el.firstChild) {
          this.el.removeChild(this.el.firstChild);
        }
        this.el.appendChild(this.compiledTemplate.toDom(dataItem));
      }
      // Run attachView with this instance to attach childView widget points
      this.compiledTemplate = this.compiledTemplate.attachView(this, ViewWidget);

      // If the deferRender option was set, it means a layout manager / a module will control when this view is rendered
      if (!this.options.deferRender) {
        var self = this;
        setTimeout(function() {
          self.vtree = self.vtree || self.compiledTemplate.toVdom(dataItem);
          self.attachChildViews();
        }, 0);
      }
    }

    this.initializeRenderListener(dataItem);
    this.postInitialize();
  },
  tungstenViewInstance: true,<commit_msg>Change to dynamicInitialize to set initial vtree now that childView attachments are deferred<commit_after>/**
 * Base backbone view for vdom- see class declaration for more information
 *
 * @author    <NAME> <<EMAIL>>
 */
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var tungsten = require('../../src/tungsten');
var ViewWidget = require('./backbone_view_widget');

// Cached regex to split keys for `delegate`.
var delegateEventSplitter = /^(\S+)\s*(.*)$/;

/**
 * Provides generic reusable methods that child views can inherit from
 */
var BaseView = Backbone.View.extend({
  /*
   * Default to an empty hash
   */
  eventOptions: {},
  /**
   * Shared init logic
   */
  initialize: function(options) {
    if (!this.el) {
      return false;
    }
    this.options = options || {};

    // Pass router through options, setting router.view if not already set
    if (this.options.router) {
      this.router = this.options.router;
      this.router.view = this.router.view || this;
    }
    // Template object
    if (this.options.template) {
      this.compiledTemplate = this.options.template;
    }
    // VTree is passable as an option if we are transitioning in from a different view
    if (this.options.vtree) {
      this.vtree = this.options.vtree;
    }
    // First-pass rendering context
    if (this.options.context) {
      this.context = this.options.context;
    }
    // Handle to the parent view
    if (this.options.parentView) {
      this.parentView = this.options.parentView;
    }

    var dataItem = this.serialize();

    // Sanity check that compiledTemplate exists and has a toVdom method
    if (this.compiledTemplate && this.compiledTemplate.toVdom) {
      if (this.options.dynamicInitialize) {
        // If dynamicInitialize is set, empty this.el and replace it with the rendered template
        // @TODO set this.vtree to an empty node and just call render?
        while (this.el.firstChild) {
          this.el.removeChild(this.el.firstChild);
        }
        var template = this.compiledTemplate;
        var nestingDepth = 0;
        if (!this.parentView) {
          template = template.wrap(this.el.nodeName);
          nestingDepth = 1;
        }
        this.vtree = template.toVdom(dataItem);
        // toDOM returns a DocumentFragment so firstChild will be the div
        var wrappedTemplate = tungsten.toDOM(this.vtree);
        while (nestingDepth--) {
          wrappedTemplate = wrappedTemplate.firstChild;
        }
        while (wrappedTemplate.firstChild) {
          this.el.appendChild(wrappedTemplate.firstChild);
        }
      }
      // Run attachView with this instance to attach childView widget points
      this.compiledTemplate = this.compiledTemplate.attachView(this, ViewWidget);

      // If the deferRender option was set, it means a layout manager / a module will control when this view is rendered
      if (!this.options.deferRender) {
        var self = this;
        setTimeout(function() {
          self.vtree = self.vtree || self.compiledTemplate.toVdom(dataItem);
          self.attachChildViews();
        }, 1);
      }
    }

    this.initializeRenderListener(dataItem);
    this.postInitialize();
  },
  tungstenViewInstance: true,
{'fraction_non_alphanumeric': 0.11116751269035532, 'fraction_numerical': 0.0006768189509306261, 'mean_word_length': 2.6263803680981597, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '901357', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1653, 'n_words': 484}