addRecord method of store

LabKey Support Forum (Inactive)
addRecord method of store Ben Bimber  2011-06-15 03:31
Status: Closed
 
Hi Jen,

your example looks fine, although I dont know the structure of study.demographics. have you used firebug before? chrome's JS debugger would also work. i'd highly recommend using one here. you can apply a breakpoint at the first line of the addRecord() method and talk through it line by line to see what's not working. if this doesnt make sense, either I or someone from labkey could probably say more.


trent - i believe your issue with the store and getModifiedRecords() is something with ext versions. if you look at the Ext code in labkey, Ext.data.Store.insert() uses:

insert : function(index, records){
records = [].concat(records);
for(var i = 0, len = records.length; i < len; i++){
this.data.insert(index, records[i]);
records[i].join(this);
}
if(this.snapshot){
this.snapshot.addAll(records);
}
this.fireEvent('add', this, records, index);
},


however, the ext 3.2 doc shows this:

    insert : function(index, records) {
        var i, len, record;
        
        records = [].concat(records);
        for (i = 0, len = records.length; i < len; i++) {
            record = records[i];
            
            this.data.insert(index + i, record);
            record.join(this);
            
            if (record.dirty || record.phantom) {
                this.modified.push(record);
            }
        }
        
        if (this.snapshot) {
            this.snapshot.addAll(records);
        }
        
        this.fireEvent('add', this, records, index);
    },


a similar thing goes for the store's add() method. the difference is that if your newly inserted record is phantom (aka not existing on the server) it is supposed to be getting tracked as dirt, which would cause it to be returned by the store's getModifiedRecords() method. you could do something like:

Ext.override(Ext.data.Store, {
    add : function(records) {
        var i, len, record, index;
        
        records = [].concat(records);
        if (records.length < 1) {
            return;
        }
        
        for (i = 0, len = records.length; i < len; i++) {
            record = records[i];
            
            record.join(this);
            
            if (record.dirty || record.phantom) {
                this.modified.push(record);
            }
        }
        
        index = this.data.length;
        this.data.addAll(records);
        
        if (this.snapshot) {
            this.snapshot.addAll(records);
        }
        
        this.fireEvent('add', this, records, index);
    },
insert : function(index, records) {
        var i, len, record;
        
        records = [].concat(records);
        for (i = 0, len = records.length; i < len; i++) {
            record = records[i];
            
            this.data.insert(index + i, record);
            record.join(this);
            
            if (record.dirty || record.phantom) {
                this.modified.push(record);
            }
        }
        
        if (this.snapshot) {
            this.snapshot.addAll(records);
        }
        
        this.fireEvent('add', this, records, index);
    }
});

and the store will probably work correct. note, all of this is just typed into the browser and untested.