addRecord method of store

LabKey Support Forum (Inactive)
addRecord method of store jdutra  2011-06-13 12:37
Status: Closed
 
I have the following code:

_store = new LABKEY.ext.Store({
        schemaName: 'study',
        queryName: 'Demographics',
        updatable: true,
        columns: 'ParticipantId, OrigID'
    });


which successfully creates a data store. I'd like to add a record to the store. I've tried the following:

_store.addRecord({'ParticipantId': 'hello', 'OrigID': 'goodbye'});

and, just to try the simplest case,

_store.addRecord({});

but my javascript code always hangs on that line. Apparently there is something wrong with the way I'm attempting to call addRecord. Is there any way someone could give me a little example of how it works?

Thanks,
Jen
 
 
trent responded:  2011-06-14 23:14
Did you try specifying an index? I was having a play around and was having difficulty until I passed the second index attribute. Oddly, it seems to work without it now, so not sure what was going on there :/

https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.ext.Store.html#addRecord

I still didn't manage to get the changes to commit though, the commitChanges function retrieves modified records (_store.getModifiedRecords) but its returning an empty array (so just returning false and not saving the new records)... I can only assume this is intended for updating existing records and not adding new records (though it does seem a bit odd having the addRecord function if you can't save said records).

You probably already know, but the Javascript API tutorial demonstrates inserting records by using the LABKEY.Query object.

Code from the first part of the tutorial which inserts records: https://www.labkey.org/wiki/home/Study/demo/source.view?name=reagentRequest

Reference to LABKEY.Query: https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Query.html
 
Ben Bimber responded:  2011-06-15 03:31
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.