PDA

View Full Version : Does Extjs4.2 support Ext.data.ArrayReader?



softball
11 Aug 2014, 5:03 PM
Dear ?
I use Extjs 4.2.1
I want to try " Ext.data.ArrayReader" as below.
I it show Error at " var myRecord = Ext.data.Record.create ".
the message:
Uncaught TypeError: Cannot read property 'items' of undefined

Does Extjs4.2 support Ext.data.ArrayReader?



Ext.onReady(function(){
var cities = [["1", "A01"], ["2", "A02"], ["3", "A03"], ["4", "A04"]];
var mp = new Ext.data.MemoryProxy(cities);

var myRecord = Ext.data.Record.create(
[{ name:'cid', type:'int', mapping:0 },
{ name:'cname', type:'string', mapping:1 }
]
);
var myReader = new Ext.data.ArrayReader({}, myRecord);

var store = new Ext.data.Store({
proxy: mp,
reader: myReader
});

console.log(store.getCount());
});

Gary Schlosberg
11 Aug 2014, 5:59 PM
There's an example on the doc page that should help.
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.reader.Array

softball
13 Aug 2014, 5:53 PM
Dear : Thx for your help! but as the code bellow?
it show count=0. Is there some thing wrong?
THX.




Employee = Ext.define('Employee', {
extend: 'Ext.data.Model',
fields: [
'id',
{name: 'name', mapping: 1}, // "mapping" only needed if an "id" field is present which
{name: 'occupation', mapping: 2} // precludes using the ordinal position as the index.
]
});

var emp = [ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ];

//var mp = new Ext.data.MemoryProxy(emp);

var myReader = new Ext.data.reader.Array({
model: 'Employee'
}, emp);



var store = new Ext.data.Store({
proxy: { type: 'memory'}
,reader: myReader
});

console.log(store.count());

skirtle
13 Aug 2014, 7:24 PM
The example in the docs is awful.

Try this:

8sd

softball
14 Aug 2014, 1:15 AM
Dear?
Thx, You use Ext.data.Store Directory.
It doesn;t use

new Ext.data.reader.Array to build proxy naymore?

THX!

skirtle
14 Aug 2014, 6:45 AM
This bit creates an Ext.data.reader.Array:


reader: {
type: 'array'
}

You shouldn't need to create one directly but it would be something like this:


new Ext.data.reader.Array({
model: 'Employee'
});

In the docs it passes the model twice, which is wrong.

softball
14 Aug 2014, 4:38 PM
Dear skirtle?
Thank for your help.

1.so, Now does't use ?

new Ext.data.reader.Array({ model: 'Employee' });
just use the sample your writed.

i had try to use new Ext.data.reader.Array({ model: 'Employee' }) + new Ext.data.Store()
But , Don't work.

2.Does other "Reader" are the same with your sample ?

THX

skirtle
14 Aug 2014, 6:11 PM
See the docs for Json reader, they're much better. Configuring an Array reader works just the same way. The reader class is set via the type config, which matches the class alias.

Configuring proxies and readers is always done via configs rather than by directly instantiating them yourself.

Your original example seemed to be based on ExtJS 3 or earlier. Almost every line is wrong in some way from an ExtJS 4 perspective. Follow the examples given in the docs for Json reader and you can't go far wrong.

softball
14 Aug 2014, 9:12 PM
Dear skirtle? Thank you very much .

softball
15 Aug 2014, 12:57 AM
Sorry?
can I ask you another question?
as th code bellow?
I use store.load ,and I want to statr from 2 ?get 1 row.
But,It doesn't work?
store.getAt(0).get('name') show =>Bill
store.count() ==>5



Ext.define('Employee',{
extend: 'Ext.data.Model',
fields: [
{name:'id', type:'int' ,mapping:0 },
{name:'name', type:'string',mapping:1 },
{name:'occupation', type:'string',mapping:2 }
]
});



var dataSource=[[1, 'Bill', 'Gardener']
,[2, 'Ben', 'Horticulturalist']
,[3, 'Ben3', 'Horticulturalist3']
,[4, 'Ben4', 'Horticulturalist4']
,[5, 'Ben5', 'Horticulturalist5']
];
var store = new Ext.data.Store({
model: 'Employee',
data: dataSource,
proxy: {
type: 'memory',
reader: {
type: 'array'
}
}
});


store.load({
start:3,
limit:1
});


console.log(store.getAt(0).get('name'));
console.log(store.count());

mitchellsimoens
15 Aug 2014, 3:29 AM
You don't need to use store.load, memory proxy does not support paging, you can use the ux to support paging: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.ux.data.PagingMemoryProxy

softball
17 Aug 2014, 6:56 PM
Dear mitchellsimoens?
I chage the code as below:
but, doen't work!
THX!



Ext.define('Employee',{
extend: 'Ext.data.Model',
fields: [
{name:'id', type:'int' ,mapping:0 },
{name:'name', type:'string',mapping:1 },
{name:'occupation', type:'string',mapping:2 }
]
});



var dataSource=[[1, 'Bill', 'Gardener']
,[2, 'Ben1', 'Horticulturalist1']
,[3, 'Ben3', 'Horticulturalist3']
,[4, 'Ben4', 'Horticulturalist4']
,[5, 'Ben5', 'Horticulturalist5']
];

var store = new Ext.data.Store({
model: 'Employee',
data: dataSource,
proxy: {
type: 'pagingmemory',
reader: {
type: 'array'
},
enablePaging :true,
params:{start:2,limit:2}
}
});

console.log(store.getAt(0).get('name'));
console.log(store.count());

skirtle
17 Aug 2014, 10:16 PM
We can't help you if you give vague descriptions like 'doesn't work'. What do you see? Is there an error or do you just see the wrong data?

Have you included the file for the PagingMemoryProxy?

Passing URL params to a memory-based proxy doesn't make sense. Try calling loadPage instead and configure a suitable pageSize.

You shouldn't need the setting enablePaging if you're using the PagingMemoryProxy. However, for very simple paging requirements you may find it sufficient to use enablePaging with a normal MemoryProxy.

softball
17 Aug 2014, 11:14 PM
Dear skirtle:


Sorry?I doesn't describe it clearly.
1.I had include PagingMemoryProxy.js
<script type="text/javascript" src="../ext/examples/ux/data/PagingMemoryProxy.js"></script>

2.The code as below?But it show store.count()=0?
SO, I think it doesn't work for paging!!


Ext.define('Employee',{
extend: 'Ext.data.Model',
fields: [
{name:'id', type:'int' ,mapping:0 },
{name:'name', type:'string',mapping:1 },
{name:'occupation', type:'string',mapping:2 }
]
});



var dataSource=[[1, 'Bill', 'Gardener']
,[2, 'Ben1', 'Horticulturalist1']
,[3, 'Ben3', 'Horticulturalist3']
,[4, 'Ben4', 'Horticulturalist4']
,[5, 'Ben5', 'Horticulturalist5']
,[6, 'Ben6', 'Horticulturalist6']
,[7, 'Ben7', 'Horticulturalist7']
];

var store = new Ext.data.Store({
model: 'Employee',
data: dataSource,
proxy: {
type: 'pagingmemory',
reader: {type: 'array' }
},
pageSize:2,
start:2,
limit:2
});

// store.loadPage(1);
//console.log(store.getAt(0).get('name'));
console.log(store.count());


3.I had chage type: 'pagingmemory', to type: 'memory',
it show count=7.
So ,with "memory"?It work (without pageSize and start param for it).

skirtle
17 Aug 2014, 11:32 PM
PagingMemoryProxy is asynchronous. See the source. It defers its callback.

start and limit aren't valid configs for a store. Whichever proxy you use you need to set the pageSize to 1 and then call loadPage with the appropriate 'row' number.

All of this is a very strange way to read a single entry from an array.

softball
18 Aug 2014, 4:55 PM
Dear?
I changed as:


var store = new Ext.data.Store({
model: 'Employee',
data: dataSource,
proxy: {
type: 'pagingmemory',
reader: {type: 'array' },
pageSize:2
}
});

store.loadPage(2);
console.log(store.count());


it show 0.

the loadpage source code has start and limit, it seems from load()?
but, I don't need to use load?
I am so confused.

I read a book ,it said that pagingmemory can split rows by start and limit,So i want to try it.

skirtle
18 Aug 2014, 5:15 PM
As I mentioned previously, PagingMemoryProxy is asynchronous. This means that the contents will not be loaded immediately if you call loadPage. You will have to wait at least 10ms before checking the contents of the store.

When a store wants to load (or save) data it bundles up a description of the target data in an Operation and passes that to its proxy. It is the proxy's job to interpret the Operation and load the appropriate data. The Operation can have start and limit properties. For an Ajax proxy these will be translated into parameters on the request. For memory-based proxies it is up to the proxy to interpret those properties directly when reading the raw data. You do not need to set the start and limit on the Operation yourself, they will be set automatically based on the configured pageSize and the page number you pass to loadPage.