PDA

View Full Version : Adding Rows to Store w/ JsonReader



sberringer
13 Sep 2007, 8:36 PM
Can I add rows to an Ext.data.Store when it's using a JsonReader? I can .getById() on rows 1 and 2 but not the newly added {?} row.



var RecordDef = Ext.data.Record.create([
{id: 'id'}, {name: 'name'}
]);
var myReader = new Ext.data.JsonReader({
root: "rows",
id: "id"
}, RecordDef);

var jobj = { 'rows': [
{ 'id': 1, 'name': 'Bill'},
{ 'id': 2, 'name': 'Ben'} ]
};

myStore = new Ext.data.Store({
reader : myReader,
data : jobj
});

var myNewRecord = new RecordDef([{'id' :3, 'name' : 'Bob'}]);
myStore.add(myNewRecord);

var r = myStore.getById(3); // <- WORKS for 1 & 2 BUT NOT 3
if (r == undefined)
alert('not found');
else
alert(r.get('name'));


Thank You.

Animal
13 Sep 2007, 10:48 PM
var myNewRecord = new RecordDef([{'id' :3, 'name' : 'Bob'}]);


See constructor of Record: http://extjs.com/deploy/ext/docs/output/Ext.data.Record.html

sberringer
14 Sep 2007, 7:28 AM
I'm sorry I'm just not seeing it. I took my code right from the sample code in the Ext.data.Record documentation:


var TopicRecord = Ext.data.Record.create(
{name: 'title', mapping: 'topic_title'},
{name: 'author', mapping: 'username'},
{name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
{name: 'lastPost', mapping: 'post_time', type: 'date'},
{name: 'lastPoster', mapping: 'user2'},
{name: 'excerpt', mapping: 'post_text'}
);

var myNewRecord = new TopicRecord({
title: 'Do my job please',
author: 'noobie',
totalPosts: 1,
lastPost: new Date(),
lastPoster: 'Animal',
excerpt: 'No way dude!'
});
myStore.add(myNewRecord);

The only differrence was I had wrapped JSON text in brackets. I removed the brackets and still no go.


var myNewRecord = new RecordDef({'id' :3, 'name' : 'Bob'});

Animal
14 Sep 2007, 7:30 AM
When you say "no go", what does that mean?

What does the result of that call look like? Stop in Firebug and debug it.

Animal
14 Sep 2007, 7:32 AM
Read: http://extjs.com/deploy/ext/docs/output/Ext.data.Record.html#create

sberringer
14 Sep 2007, 7:33 AM
Following the rule of finding the answer right after the second posting to make yourself look more foolish. This did it.


var myNewRecord = new RecordDef({'id' :3, 'name' : 'Bob'}, 3);

I had tried this before, but it was when I had the brackets on the JSON text.

Thanks for the quick response.

Animal
14 Sep 2007, 7:38 AM
And when you've figured that out, use the constructor properly: http://extjs.com/deploy/ext/docs/output/Ext.data.Record.html

Animal
14 Sep 2007, 7:39 AM
Beat me to it!