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();
                },
                scope: this
            });
        }
    },

    save: function(syncQueue)
    {
        if (this.gridController.grid.getStore()) {
            if (syncQueue) {
                syncQueue.add(this.gridController.grid.getStore());
                if (this.reloadRowOnSave) {
                    syncQueue.on('finished', function() {
                        this._reloadLoadedRow();

                    }, this, { single: true });
                }
            } else {
                this.gridController.grid.getStore().sync({
                    callback: function() {
                        if (this.reloadRowOnSave) {
                            this._reloadLoadedRow();
                        }
                    },
                    scope: this
                });
            }
        }
    },

    getLoadedRecord: function()
    {
        return this._loadedRecord;
    },

    enable: function()
    {
        this.gridController.grid.enable();
    },
    disable: function()
    {
        this.gridController.grid.disable();
    },
    getPanel: function()
    {
        return this.grid;
    }
});

@@ -891,32 +891,1998 @@
     %7D%0A        %7D%0A
+%0A        //if both model has HayMany assocication and child model has BelongsTo associacion%0A        //we set the child models the parent model instance wich they will use for getXxx%0A        //that way both share the same object%0A        var belongsToAssoc;%0A        store.model.prototype.associations.each(function(assoc) %7B%0A            if (assoc instanceof Ext4.data.association.BelongsTo) %7B%0A                Ext4.ClassManager.get(assoc.model).prototype.associations.each(function(i) %7B%0A                    if (i instanceof Ext4.data.association.HasMany%0A                        && i.model == store.model.$className%0A                        && i.foreignKey == assoc.foreignKey%0A                    ) %7B%0A                        belongsToAssoc = assoc;%0A                    %7D%0A                %7D, this);%0A%0A            %7D%0A        %7D, this);%0A        if (belongsToAssoc) %7B%0A            if (!store%5B'belongsTo'+belongsToAssoc.instanceName%5D) %7B%0A                store.loadRecords = Ext4.Function.createInterceptor(store.loadRecords, function(records) %7B%0A                    var store = this;%0A                    for (var i=0; i %3C records.length; i++) %7B%0A                        records%5Bi%5D%5BbelongsToAssoc.instanceName%5D = store%5B'belongsTo'+belongsToAssoc.instanceName%5D;%0A                    %7D%0A                %7D);%0A                store.insert = Ext4.Function.createInterceptor(store.insert, function(index, records) %7B%0A                    var store = this;%0A                    if (!Ext4.isIterable(records)) %7B%0A                        records = %5Brecords%5D;%0A                    %7D%0A                    for (var i=0; i %3C records.length; i++) %7B%0A                        records%5Bi%5D%5BbelongsToAssoc.instanceName%5D = store%5B'belongsTo'+belongsToAssoc.instanceName%5D;%0A                    %7D%0A                %7D);%0A            %7D%0A            store%5B'belongsTo'+belongsToAssoc.instanceName%5D = row;%0A            store.each(function(r) %7B%0A                r%5BbelongsToAssoc.instanceName%5D = row;%0A            %7D, this);%0A        %7D%0A%0A
         this.gri

set parent model when loading HasMany associations