PDA

View Full Version : Working with an empty store



thrashee
26 Feb 2009, 1:41 PM
I'm having a hard time understanding the interaction and process between a store, records, and the underlying data.

Here's my scenario:

I am using an editor grid that is initally empty on render. Using an Add button, I'd like a new row to be inserted, which creates a new record in the store. I'm using JSON for the data type.

Now, I'd like to store new records in the grid in memory until a submit button is pushed, which will actually take the records as a serialized JSON object and pass this into a C# function to store in a SQL backend (not via page methods or http proxy, but using window.external to call the C# function).

My question is, how do you set up the data property of the store object? All the examples I see are actually loading some array, either through an http proxy or a dummy inline array or xml file--even if the grid starts off empty. I'm not quite understanding why you need to load a dummy set of data into the store if you're actually ADDING new records instead of reading the dummy data?

jsakalos
26 Feb 2009, 2:53 PM
Here is an example: http://recordform.extjs.eu/ There is a lot more in that example so concentrate please on add record and submit - you can safely ignore the rest.

thrashee
26 Feb 2009, 3:16 PM
Thank you for posting the example, but unfortunately the example is still using an HTTP proxy to load data.

My question is, how do you initialize a store with what's essentially an empty data object? I don't want the data object to be tied to any backing store (XML, proxy, etc), I simply want the data to be retained in memory, as perhaps an empty (initially) array object.

Do you HAVE to have some sort of pre-filled data object in order to initialize the store? Is there no way of having an empty array as your data object, which then gets filled as you add records?

mjlecomte
26 Feb 2009, 3:31 PM
What about using a SimpleStore with a JsonReader? Actually just Store I guess.

thrashee
26 Feb 2009, 6:39 PM
I'm actually using a store right now.

Forgive my ignorance, but doesn't a store have to have a backing data object? Moving from the .Net world, the store seems roughly analogous to a data adapter.

If I leave out any type of data specification for the store, I wind up with a "data is null" error when trying to add a record.

This is truly baffling me....all I want to do is add new records to the store, and eventually extract them from the store to ship off via a direct call to C# code. The examples are great as long as they accurately compare to your given situation, but if they don't, the documentation and API are unfortunately lacking in elaborating on exactly what is required to do what.

jsakalos
27 Feb 2009, 2:06 AM
Is that what you are after a store without records?

mjlecomte
27 Feb 2009, 3:56 AM
I'm actually using a store right now.

Forgive my ignorance, but doesn't a store have to have a backing data object? Moving from the .Net world, the store seems roughly analogous to a data adapter.

If I leave out any type of data specification for the store, I wind up with a "data is null" error when trying to add a record.

This is truly baffling me....all I want to do is add new records to the store, and eventually extract them from the store to ship off via a direct call to C# code. The examples are great as long as they accurately compare to your given situation, but if they don't, the documentation and API are unfortunately lacking in elaborating on exactly what is required to do what.

What about just putting data=[] ?
Docs are always improving through suggestions. So if you see something lacking in the docs and have an idea to improve them to make something more clear, speak up.

thrashee
27 Feb 2009, 5:43 AM
Thanks everyone for your replies.

Yes, I just wanted an initially empty store, to which new records would be added. I've finally gotten it up and running via a MemoryProxy and adding records via a function.

xpuu
17 Apr 2009, 4:46 AM
Btw. I am really interested to see final solution, because I am solving same problem. Empty store, what a challenge!

And, no offense, I totally agree with this sentence - The examples are great as long as they accurately compare to your given situation, but if they don't, the documentation and API are unfortunately lacking in elaborating on exactly what is required to do what.

mjlecomte
17 Apr 2009, 5:30 AM
I have yet to see anyone post any suggestions. The API Docs are never going to show every possible situation out there. Could they have more examples or explanation? Sure, but it really helps if someone makes a suggestion.

Someone updating the docs is left to try to imagine what possible scenario you have and also where someone even looked to find information. The prior poster made his complaint, got his solution, and didn't bother to make suggestions or even post his solution.

As a guess I threw together this mock up:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Empty store test</title>

<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />

<script type="text/javascript" src="../../adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../ext-all-detail.js"></script>

<script type="text/javascript">
Ext.BLANK_IMAGE_URL = '../../resources/images/default/s.gif';

Ext.onReady(function(){

/*
// Option 1: choose a store that implicitly creates a reader commensurate to the data object
var myStore = new Ext.data.ArrayStore({
fields: ['fullname', 'first'],
});
*/
// Option 2: use the generic store and then explicitly create the reader
var rt = Ext.data.Record.create([
{name: 'fullname'},
{name: 'first'}
]);
var myStore = new Ext.data.Store({
reader: new Ext.data.ArrayReader({id: 0}, rt)
});

// current store data count
console.info('store count:',myStore.getCount());

// load some data into store
var myData = [
[1, 'Fred Flintstone', 'Fred'],
[2, 'Barney Rubble', 'Barney']
];
myStore.loadData(myData);

// updated store count
console.info('store count:',myStore.getCount());

// add a record
var defaultData = {
fullname: 'Full Name',
first: 'First Name'
};
var recId = 100; // provide unique id
var r = new myStore.recordType(defaultData, ++recId); // create new record
myStore.insert(0, r); // add new record to the store

// updated store count
console.info('store count:',myStore.getCount());

// global variables for testing with:
d = myData;
s = myStore;
console.info(s);
});
</script>

</head>
<body></body>
</html>

xpuu
17 Apr 2009, 5:31 AM
Okay I finally done it with empty JsonStore, but I am really wondering why is so uneasy to use just basic Ext.data.Store for these kind of things...

Example
http://jsbin.com/utevu/edit

Condor
17 Apr 2009, 5:37 AM
The store needs to know how to handle the data. This is why a store needs a reader, e.g.

var store = new Ext.data.Store({
reader: new Ext.data.JsonReader({
fields: ['field1', 'field2']
}),
data: [
{field1: 'A', field2: 'B'},
{field1: 'C', field2: 'D'},
{field1: 'E', field2: 'E'}
]
});
Compare with:

var store = new Ext.data.Store({
reader: new Ext.data.JsonReader({
fields: [
{name: 'field1', mapping: '0'},
{name: 'field2', mapping: '1'},
]
}),
data: [
['A', 'B'],
['C', 'D'],
['E', 'F']
]
});
(which could also have been done with an ArrayReader)