<commit_before>/// Backbonize a metamodel, set default values for model
///
/// @return Constructor of Backbone model
function backbonizeModel(metamodel) {
    var defaults = {};
    var fieldHash = {};
    _.each(metamodel.fields,
          function(f) {
              if (!(_.isUndefined(f.default)))
                  defaults[f.name] = f.default;
              else
                  defaults[f.name] = null;
              fieldHash[f.name] = f;
          });

    var M = Backbone.Model.extend({
        defaults: defaults,
        dirtyAttributes: {},
        initialize: function() {
            if (!this.isNew())
                this.fetch();
        },
        metamodel: metamodel,
        fieldHash: fieldHash,
        /// Bind model changes to server sync
        setupServerSync: function () {
            var realUpdates = function () {
                /// Do not resave model when id is set after
                /// first POST
                if (!this.hasChanged("id"))
                    this.save();
            };

            this.bind("change", _.throttle(realUpdates, 500), this);
        },
        set: function(attrs, options){
            Backbone.Model.prototype.set.call(this, attrs, options);
            /// Queue new values to be saved on server using
            /// dirtyAttributes
            ///
            /// TODO _.extend doesn't work here
            for (k in attrs)
                this.dirtyAttributes[k] = attrs[k];
        },
        /// For checkbox fields, translate "0"/"1" to false/true
        /// boolean.
        parse: function(json) {
            var m = this.metamodel;
            for (k in json) {
                if (this.fieldHash[k].type == "checkbox") {
                    if (json[k] == "1")
                        json[k] = true;
                    else
                        json[k] = false;
                }
            }
            return json;
        },        
        toJSON: function () {
            /// Send only dirtyAttributes instead of the whole object
            json = this.dirtyAttributes;
            /// Map boolean values to string "0"/"1"'s for server
            /// compatibility
            for (k in json)
                if (_.isBoolean(json[k]))
                    json[k] = String(json[k] ? "1" : "0");
            this.dirtyAttributes = {};
            return json;
        },
        urlRoot: "."
    });

    return M;
}

DefaultFieldType = "text";

/// Convert metamodel to forest of HTML form elements with appropriate
/// data-bind parameters for Knockout.
///
/// TODO: We can do this on server as well.
///
/// @return String with form HTML
function renderFormView(metamodel) {
    var templates = [];

    _.each($(".field-template"),
           function(tmp) {
               templates[/\w+/.exec(tmp.id)[0]] = tmp.text;
           });

    var contents = "";
    var fType = "";
    /// Pick an appropriate form widget for each metamodel
    /// field type and render actual model value in it
    _.each(metamodel.fields,
           function (f) {
               if (_.isUndefined(templates[f.type]))
                   fType = "unknown";
               else
                   fType = f.type;
               contents += Mustache.render(templates[fType],
                                           _.extend(f, {readonly: !f.canWrite}));
           });

    return contents;
}<commit_msg>Do not try to do "0"/"1" to boolean conversion on "id" attribute<commit_after>/// Backbonize a metamodel, set default values for model
///
/// @return Constructor of Backbone model
function backbonizeModel(metamodel) {
    var defaults = {};
    var fieldHash = {};
    _.each(metamodel.fields,
          function(f) {
              if (!(_.isUndefined(f.default)))
                  defaults[f.name] = f.default;
              else
                  defaults[f.name] = null;
              fieldHash[f.name] = f;
          });

    var M = Backbone.Model.extend({
        defaults: defaults,
        dirtyAttributes: {},
        initialize: function() {
            if (!this.isNew())
                this.fetch();
        },
        metamodel: metamodel,
        fieldHash: fieldHash,
        /// Bind model changes to server sync
        setupServerSync: function () {
            var realUpdates = function () {
                /// Do not resave model when id is set after
                /// first POST
                if (!this.hasChanged("id"))
                    this.save();
            };

            this.bind("change", _.throttle(realUpdates, 500), this);
        },
        set: function(attrs, options){
            Backbone.Model.prototype.set.call(this, attrs, options);
            /// Queue new values to be saved on server using
            /// dirtyAttributes
            ///
            /// TODO _.extend doesn't work here
            for (k in attrs)
                this.dirtyAttributes[k] = attrs[k];
        },
        /// For checkbox fields, translate "0"/"1" to false/true
        /// boolean.
        parse: function(json) {
            var m = this.metamodel;
            for (k in json) {
                if ((k != "id") && (this.fieldHash[k].type == "checkbox")) {
                    if (json[k] == "1")
                        json[k] = true;
                    else
                        json[k] = false;
                }
            }
            return json;
        },        
        toJSON: function () {
            /// Send only dirtyAttributes instead of the whole object
            json = this.dirtyAttributes;
            /// Map boolean values to string "0"/"1"'s for server
            /// compatibility
            for (k in json)
                if (_.isBoolean(json[k]))
                    json[k] = String(json[k] ? "1" : "0");
            this.dirtyAttributes = {};
            return json;
        },
        urlRoot: "."
    });

    return M;
}

DefaultFieldType = "text";

/// Convert metamodel to forest of HTML form elements with appropriate
/// data-bind parameters for Knockout.
///
/// TODO: We can do this on server as well.
///
/// @return String with form HTML
function renderFormView(metamodel) {
    var templates = [];

    _.each($(".field-template"),
           function(tmp) {
               templates[/\w+/.exec(tmp.id)[0]] = tmp.text;
           });

    var contents = "";
    var fType = "";
    /// Pick an appropriate form widget for each metamodel
    /// field type and render actual model value in it
    ///
    /// Set readonly context attribute for fields which have
    /// canEdit=false in form description.
    _.each(metamodel.fields,
           function (f) {
               if (_.isUndefined(templates[f.type]))
                   fType = "unknown";
               else
                   fType = f.type;
               contents += Mustache.render(templates[fType],
                                           _.extend(f, {readonly: !f.canWrite}));
           });

    return contents;
}
{'fraction_non_alphanumeric': 0.12346032655399598, 'fraction_numerical': 0.0034374104841019765, 'mean_word_length': 1.3511784511784513, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '342267', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1773, 'n_words': 515}