PDA

View Full Version : Model load problem



tafali
8 Mar 2012, 12:05 PM
i have an object :


sinif = {
isim :"3a",
ogrenciler:[
{
id:1,
ogrIsim:"mustafa",
yas:23373,
no:12
},{
id:2,
ogrIsim:"ahmet",
yas:24,
no:15
}
]
}


and i have a model:



Ext.define("Tf.model.Ogrenci",
{
extend:"Ext.data.Model",
fields:[
{name: 'id', type: 'int'},
{name:"isim", type:"string", mapping:"ogrIsim"},
{name:"yas", type:"int"}
],
proxy:{
data: sinif,
type:"memory",
reader:{
type:"json",
root:"ogrenciler"
}
}
}
);


when i want to load data;



var AA = Ext.ModelManager.getModel('Tf.model.Ogrenci');

AA.load(2/*or 6 or anything */,
{
success: function(ogr) {
console.log( ogr.data );
}
});


it always loads first data ( id=1 ) .

where is my mistake?

thanks..

vietits
8 Mar 2012, 4:12 PM
Memory proxy always read data from its 'data' property when you call load() method regardless arguments you passed. Because your data does not change so the results will be the same.

tafali
9 Mar 2012, 12:05 AM
thanks your answer,

but i moved data to "sinif.json" file on server and try it as "ajax proxy", like this:


proxy:{
url:"sinif.json",
type:"ajax",
reader:{
type:"json",
root:"ogrenciler"
}
}


and same problem occurs yet.


...
AA.load(8 ,
{
success: function(ogr) {
console.log( ogr.data );
}
});


firebug console is like this.


GET http://****/test/sinif.json?_dc=1331279585623&id=8

{
isim :"3a",
ogrenciler:[
{
id:1,
ogrIsim:"mustafa",
yas:23373,
no:12
},{
id:2,
ogrIsim:"ahmet",
yas:24,
no:15
}
]
}


its answer is this:


Object { id=1, isim="mustafa", yas=23373}


is it true to say
"if there are more then one records in 'data' then it (model) gets first record. for stuation like this (more than one record) , we shouldn't use model to load, instead of that we should use store." ?

börn
9 Mar 2012, 12:09 AM
when the reply of the server is a record with id=1 instead of 8... - so I suggest there is something rotten on the server? ;) - check which data is really sent by the server... (do you inspect the id-parameter on the server method?)

vietits
9 Mar 2012, 12:14 AM
The problem is still the same. Each time you call load() it returns the same data regardless what you passed as arguments. The load() sends request to your server side only. You should write a script on your server side to handle the request and returns the result basing on the parameters passed to it.

tafali
9 Mar 2012, 12:52 AM
:) sorry, i noticed my fault and i edited my message ( and server side script :) ).

i have asked this question:

is it true to say
"if there are more then one records in 'data' then it (model) gets first record. for stuation like this (more than one record) , we shouldn't use model to load, instead of that we should use store." ?

vietits
9 Mar 2012, 1:44 AM
is it true to say
"if there are more then one records in 'data' then it (model) gets first record. for stuation like this (more than one record) , we shouldn't use model to load, instead of that we should use store." ?

We often use store to store data (equivalent to table of records in database) while model (equivalent to table structure in database) is just for describing data structure. Sometime, we create an instance of model (equivalent to a record) in case we need only one record.

börn
9 Mar 2012, 1:47 AM
yes - a model is always one record. It's like in all other oop languages ;) - a person model is a class Person. If you want to have more then one Person you need something that gets more then one record. And this mechanism is called store in ext.

tafali
9 Mar 2012, 3:34 AM
thanks to both of you :)

i wish i'd select #7, #8 as best answer ..