<commit_before>Ext4.define('Kwf.Ext4.Controller.Bindable.Grid', {
    extend: 'Kwf.Ext4.Controller.Bindable.Abstract',

    relation: null,
    gridController: null,
    reloadRowOnSave: false,

    init: function()
    {
    },

    load: function(row)
    {
        var storeName = this.relation+'Store'; //same naming as in Ext.data.association.HasMany
        if (this._loadedRecord && this._loadedRecord[storeName]) {
            //if new row has same id as currently loaded copy the store
            //this makes sures dirty values are kept
            if (row.getId() == this._loadedRecord.getId()) {
                row[storeName] = this._loadedRecord[storeName];
            }
        }
        this._loadedRecord = row;
        var store = row[this.relation]();
        if (!store.loaded) {
            store.loaded = true;
            if (!row.phantom) {
                store.load();
            }
        }
        this.gridController.grid.bindStore(store);
    },

    reset: function()
    {
        this._loadedRecord = null;
        this.gridController.grid.unbindStore();
    },

    isDirty: function()
    {
        if (!this.gridController.grid.getStore()) return false;
        return this.gridController.grid.getStore().getModifiedRecords().length || this.gridController.grid.getStore().getNewRecords().length;
    },

    isValid: function()
    {
        return true;
    },

    _reloadLoadedRow: function()
    {
        var r = this.getLoadedRecord();
        if (!r.phantom) {
            r.self.load(r.getId(), {
                success: function(loadedRow) {
                    r.beginEdit();
                    r.set(loadedRow.getData());
                    r.endEdit();
                    r.commit();<commit_msg>Ext4: set parent model when loading HasMany associations<commit_after>Ext4.define('Kwf.Ext4.Controller.Bindable.Grid', {
    extend: 'Kwf.Ext4.Controller.Bindable.Abstract',

    relation: null,
    gridController: null,
    reloadRowOnSave: false,

    init: function()
    {
    },

    load: function(row)
    {
        var storeName = this.relation+'Store'; //same naming as in Ext.data.association.HasMany
        if (this._loadedRecord && this._loadedRecord[storeName]) {
            //if new row has same id as currently loaded copy the store
            //this makes sures dirty values are kept
            if (row.getId() == this._loadedRecord.getId()) {
                row[storeName] = this._loadedRecord[storeName];
            }
        }
        this._loadedRecord = row;
        var store = row[this.relation]();
        if (!store.loaded) {
            store.loaded = true;
            if (!row.phantom) {
                store.load();
            }
        }

        //if both model has HayMany assocication and child model has BelongsTo associacion
        //we set the child models the parent model instance wich they will use for getXxx
        //that way both share the same object
        var belongsToAssoc;
        store.model.prototype.associations.each(function(assoc) {
            if (assoc instanceof Ext4.data.association.BelongsTo) {
                Ext4.ClassManager.get(assoc.model).prototype.associations.each(function(i) {
                    if (i instanceof Ext4.data.association.HasMany
                        && i.model == store.model.$className
                        && i.foreignKey == assoc.foreignKey
                    ) {
                        belongsToAssoc = assoc;
                    }
                }, this);

            }
        }, this);
        if (belongsToAssoc) {
            if (!store['belongsTo'+belongsToAssoc.instanceName]) {
                store.loadRecords = Ext4.Function.createInterceptor(store.loadRecords, function(records) {
                    var store = this;
                    for (var i=0; i < records.length; i++) {
                        records[i][belongsToAssoc.instanceName] = store['belongsTo'+belongsToAssoc.instanceName];
                    }
                });
                store.insert = Ext4.Function.createInterceptor(store.insert, function(index, records) {
                    var store = this;
                    if (!Ext4.isIterable(records)) {
                        records = [records];
                    }
                    for (var i=0; i < records.length; i++) {
                        records[i][belongsToAssoc.instanceName] = store['belongsTo'+belongsToAssoc.instanceName];
                    }
                });
            }
            store['belongsTo'+belongsToAssoc.instanceName] = row;
            store.each(function(r) {
                r[belongsToAssoc.instanceName] = row;
            }, this);
        }

        this.gridController.grid.bindStore(store);
    },

    reset: function()
    {
        this._loadedRecord = null;
        this.gridController.grid.unbindStore();
    },

    isDirty: function()
    {
        if (!this.gridController.grid.getStore()) return false;
        return this.gridController.grid.getStore().getModifiedRecords().length || this.gridController.grid.getStore().getNewRecords().length;
    },

    isValid: function()
    {
        return true;
    },

    _reloadLoadedRow: function()
    {
        var r = this.getLoadedRecord();
        if (!r.phantom) {
            r.self.load(r.getId(), {
                success: function(loadedRow) {
                    r.beginEdit();
                    r.set(loadedRow.getData());
                    r.endEdit();
                    r.commit();
{'fraction_non_alphanumeric': 0.11581049192230895, 'fraction_numerical': 0.0027228172082047557, 'mean_word_length': 1.6734594856865599, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1443793', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1465, 'n_words': 289}