PDA

View Full Version : Problem adding record to jsonstore



jeisong
4 Jan 2011, 11:52 AM
Hello, i have a issue adding a record to a Store,

I have this object in java



public class Parent{
private Integer id;
private String name;
.....
Setters y Getters...
.....
}
...

AND

...


public class Child{
private Parent parent;
private Integer id;
private String name;
.....
Setters y Getters...
.....
}
When I serialize to json the childs object's collection is:



{
total: 2,
data:[
{"id":1,"name":"Jhon Doe", "parent":[{"id":1,"name":"Parent of Jhon Doe"}]},
{"id":2,"name":"Michael Jackson", "parent":[{"id":2,"name":"Parent of Michael Jackson"}]}
]
}
and my jsonReader is like this,



var myReader = new Ext.data.JsonReader({
// metadata configuration options:
idProperty: 'id'
root: 'data',
totalProperty: 'total',

fields:[
{name: 'id', mapping: 'id'},
{name: 'name', mapping: 'name'},
{name: 'parent.id', mapping: 'parent.id'},
{name: 'parent.name', mapping: 'parent.name'}
]
});
and my column model is like this,



var colModel = new Ext.grid.ColumnModel([
{ header: "id", dataIndex:"id"},
{ header: "name", dataIndex:"name"},
{ header: "Parent Id", dataIndex:"parent.id"},
{ header: "Parent Name", dataIndex:"parent.name"}
]);
So far so perfect,

the problem is when I try to add a record to the store because I try whit this:
but my grid no load the data for the Parent object



var Person = Ext.data.Record.create([
{name: 'id', mapping: 'id'},
{name: 'name', mapping: 'name',},
{name: 'parent.id', mapping: 'parent.id', type: 'int'},
{name: 'parent.name', mapping: 'parent.name', type: 'string'}
]);

var newPerson = new Person(
{
id: '',
name: 'noobie',
parent:{
id:1,
name:"new person"
}
}
);
myStore.add(newPerson);


anyone can helpme and tellme how add a record to this store?

Thanks.

darthwes
4 Jan 2011, 12:18 PM
OK, first off, I don't think you can have an array of parents as part of the record. I (should) could be wrong, and would like to learn if I am. However, my understanding is that you can do like you are showing with the parent being a json object, but you can't wrap it as an array. To do that sort of thing you would probably set up another end point to query out the parents given a person's id.

Then you should have something like


var Person = grid.store.recordType;
var newPerson = new Person({
name: 'noobie',
parent: {
name: 'new person'
}
});


The way you ask for the new id will vary on your setup, a crud-enabled store will issue a create command, you could always do a singular ajax request for the creation, and get an id back to use in your constructor. There are other ways but those are the most common that I have seen.

Also, I usually end up having to write the logic to do store.insert(idx, newObj) instead of a simple store.add(newObj) to control where I'm placing the new record. Hope that helps!