PDA

View Full Version : add new Record in Store. Red triangle, why?



fenuk
14 Nov 2010, 5:13 PM
I write this code:


Ext.onReady(function() {

var oForm = new Ext.FormPanel({
region : 'center',
labelWidth : 100,
url : 'save-form.php',
frame : true,
bodyStyle : 'padding:5px 5px 0',
width : 350,
defaults : {
width : 230
},
defaultType : 'textfield',

items : [{
fieldLabel : 'Name Artist',
name : 'name',
allowBlank : false
}, {
fieldLabel : 'Surname Artist',
name : 'surname',
allowBlank : false
}],

buttons : [{
text : 'Save',
handler : function() {

var v1 = oForm.getForm()
.getFieldValues();
var u = new oStore.recordType(v1);
oStore.add(u);
}
}, {
text : 'Cancel'
}]
})

var oWindow = new Ext.Window({

height : 500,
width : 500,
title : 'add artist',
closeAction : 'hide',
layout : 'border',
items : [oForm]
})

var proxy = new Ext.data.HttpProxy({
api : {
read : './list',
create : './save',
update : './update',
destroy : './destroy'
}
});

var reader = new Ext.data.JsonReader({
root : 'data',
// totalProperty : 'total',
successProperty : 'success',
// idProperty : 'id',
messageProperty : 'message'
}, [{
name : 'name',
allowBlank : false
}, {
name : 'surname',
allowBlank : false
}]);

var writer = new Ext.data.JsonWriter({
encode : true,
writeAllFields : false
});

var oStore = new Ext.data.Store({
// id : 'user',
proxy : proxy,
reader : reader,
writer : writer, // <-- plug a DataWriter into the store just
// as you would a Reader
autoSave : true
// <-- false would delay executing create, update, destroy
// requests until specifically told to do so with some
// [save] buton.
});

oStore.load();

var oGrid = new Ext.grid.GridPanel({

store : oStore,
tbar : [{
text : 'Add',
handler : function() {
oWindow.show()
}
}],
sortable : true,
columns : [

{
header : 'Name',
dataIndex : 'name',
width : 200
}, {
header : 'Surname',
dataIndex : 'surname',
width : 200
}],
width : 600,
height : 300,
title : 'Artist',
region : 'center',
listeners : {

'rowcontextmenu' : function(oGrid, nIndex, event) {
event.stopEvent();
menu = new Ext.menu.Menu({
items : [{
text : 'remove',
handler : function() {

console.log(oGrid
.getStore()
.removeAt(nIndex));

}
}]
});
menu.showAt(event.getXY());
}
}

})

new Ext.Viewport({

layout : 'border',
items : oGrid
})
})


all work fine, and i have only one problem: when i try add new record in store i run this code:



var v1 = oForm.getForm().getFieldValues();
var u = new oStore.recordType(v1);
oStore.add(u);

new Record add to the GridTable - with red triangle in the left corner and i see request on the server to save record.
script on the server send me result:

{"success":true}, but red triangle in new row of grid not disappear. Why?

Help me, please.

Markj
6 Dec 2010, 5:27 AM
Hi

did you figure it out? because i have an issue almost like yours

br
Mark

Condor
6 Dec 2010, 5:36 AM
Are you using a writer to save your store changes?

In that case the server also needs to return the newly created record (Ext needs to know the assigned record id).

Markj
6 Dec 2010, 5:50 AM
Im using a JsonWriter with a HTTPProxy

im currently returning the following json string (if everything goes alright)

{"success":true}

btw. I have the same problem with editing... and im also saveing/updating multiple items at a time

Condor
6 Dec 2010, 5:57 AM
No. For create, update and delete actions your server not only needs to return success:true, but also the modified records.

Markj
7 Dec 2010, 1:32 PM
hmm... I think i have tried what you said, I return every row that I submit.. but I get an exception.. im not sure why it thinks it is invalid data since the data i return on the Write action is the same as on the Read.

uncaught exception: Ext.data.DataReader: #realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.

Condor
8 Dec 2010, 12:00 AM
Does the format of the returned data for a create/update/delete match the data you return for read/load?

Markj
8 Dec 2010, 12:24 AM
I would think so?

this is from the load

{"Items":[{"clientId":"332aabfa-e3be-4f43-8bb8-9e39014844c1","productId":"332aabfa-e3be-4f43-8bb8-9e39014844c1","name":"1234","selected":false,"status":"Category A"},{"clientId":"4532e2ee-b448-4c31-91a4-9e3901488098","productId":"4532e2ee-b448-4c31-91a4-9e3901488098","name":"34234","selected":true,"status":"Category C"},{"clientId":"6d6d88e9-993e-473d-9246-9e3901488bb3","productId":"6d6d88e9-993e-473d-9246-9e3901488bb3","name":"sd fgsdfg","selected":true,"status":"Category B"},{"clientId":"e530c9d4-664a-4271-ad4d-9e390148e8fe","productId":"e530c9d4-664a-4271-ad4d-9e390148e8fe","name":" hfgjgjj56h","selected":true,"status":"Category B"}]}


this is what is return from the write

{"success":true,"message":"","Items":[{"clientId":"4532e2ee-b448-4c31-91a4-9e3901488098","productId":"4532e2ee-b448-4c31-91a4-9e3901488098","name":"34234","selected":false,"status":"Category C"},{"clientId":"6d6d88e9-993e-473d-9246-9e3901488bb3","productId":"6d6d88e9-993e-473d-9246-9e3901488bb3","name":"sd fgsdfg","selected":false,"status":"Category B"},{"clientId":"e530c9d4-664a-4271-ad4d-9e390148e8fe","productId":"e530c9d4-664a-4271-ad4d-9e390148e8fe","name":" hfgjgjj56h","selected":false,"status":"Category B"},{"clientId":"332aabfa-e3be-4f43-8bb8-9e39014844c1","productId":"332aabfa-e3be-4f43-8bb8-9e39014844c1","name":"1234","selected":false,"status":"Category A"}]}

Condor
8 Dec 2010, 12:51 AM
Looks OK. What is your idProperty?

ps. There is currently a bug in onCreateRecords that causes it to skip reMap when adding multiple records.

Markj
8 Dec 2010, 11:59 AM
it is clientId

however, is it possible to force the Writer to submit the data in Items as a Array?

because is i only change one row, the data will be send as a single object.. something like this Items:{} .. if I change to rows the data will be send as Items:[{},{}]

and it kind of breaks the code (im using aspnet mvc)

Markj
18 Jan 2011, 11:46 AM
Has this been fixed?

amarkiewicz
18 Jan 2011, 12:58 PM
it is clientId

however, is it possible to force the Writer to submit the data in Items as a Array?

because is i only change one row, the data will be send as a single object.. something like this Items:{} .. if I change to rows the data will be send as Items:[{},{}]

and it kind of breaks the code (im using aspnet mvc)

You should be able to force the writer to send a list every time by setting the "listful" property of the writer to "true".

writer: new Ext.data.JsonWriter({
encode: true,
listful: true,

Markj
18 Jan 2011, 1:08 PM
Thanks :)

Now I just need to figure out the other part

amarkiewicz
18 Jan 2011, 2:31 PM
I am working on the same problem (which is how I found the thread).
First time I am using a dataWriter.

If I understand correctly, all fields of the new record must be returned?

Markj
18 Jan 2011, 2:48 PM
Not sure what the right way is since I haven't seen it work properly yet

But Condor said something about a bug... so!

amarkiewicz
17 Feb 2011, 2:48 PM
This may be too late to help you, but maybe it still can.

I ran into this error again and decided to bite the bullet and expedite the ExtJS code to see what's going on with this DataReader.realize() error. Thanks to Sencha by the way to give us the debug version of the source code. It helps immensely.

After an insert it seems the DataReader will take the response.data object and copy those values into the datastore. One thing it does though is check if the data object is "really a data object". It determines if it is valid if the DataReader.idProperty element is in the response data. If it is not, you get the error.
If you do not explicitly specify the "idProperty" it defaults to "id".

The issue I encountered was one of case.
My DataReader.idProperty = "cmpnt_num".
While my response data was "CMPNT_NUM".

The data actually copies over just fine, but there is one more step in the ExtJS code that copies over the id. It looks exactly for DataReader.idProperty, case sensitive. It did not find that key and actually overwrote the value I returned from the database. Then the check of "is it really data" failed.

In your case it might be something similar.
I noticed your DataReader.idProperty has a commented out value. This means the idProperty the reader is looking for is "id".


var reader = new Ext.data.JsonReader({
root : 'data',
// totalProperty : 'total',
successProperty : 'success',
// idProperty : 'id',
messageProperty : 'message'Your data though does not seem to have the idPrpoerty='id' that the reader would be looking for.
If "clientId" is a PK, try specifying that for the DataReader.idProperty. Or provide an "id" attribute in your return data.

{"Items":[{"clientId":"332aabfa-e3be-4f43-8bb8-9e39014844c1","productId":"332aabfa-e3be-4f43-8bb8-9e39014844c1","name":"1234","selected":false,"status":"Category
A"},


Andrew