PDA

View Full Version : How to implement Create method (CRUD on .NET)



stalek
15 Feb 2010, 5:57 AM
Hi,
I have a store that has been configured like below:


var recordFields = [
'ElementID',
'Value',
{name:'SortOrder', type:'int'},
{name:'ExtSysID', type:'int'},
{name:'Valid', type:'boolean'}
];

var reader = new Ext.data.JsonReader({
idProperty : 'ElementID',
root : 'Result',
totalProperty : 'TotalCount',
fields : recordFields
});

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

var proxy = new Ext.data.DirectProxy({
api : {
read : DictionaryElemService.Read,
create : DictionaryElemService.Create,
update : DictionaryElemService.Update,
destroy : DictionaryElemService.Destroy
},
paramOrder : ['start', 'limit', 'sort', 'dir', 'filter', 'parentID'],
paramsAsHash : true
});

var store = new Ext.data.DirectStore({
autoLoad : false,
autoSave : false,
reader : reader,
writer : writer,
proxy : proxy,
baseParams : { start: 0, limit: 50, sort: 'SortOrder', dir: 'ASC', filter: null, parentID:null },
remoteSort : true,
sortInfo : { field: 'SortOrder', direction: 'ASC'},
listeners : {
exception : function () {
console.info(arguments);
}
}
});

I have created a new row by inserting it directly using insert() method. It has some id like 'ext-something'. When I do save operation on the store I see such value passed for the request in the POST body (there are no "id" parameters here at all !):


{"action":"DictionaryElemService","method":"Create","data":[{"start":0,"limit":50,"sort":"SortOrder","dir":"ASC","filter":null,"parentID":"a2f53a6b-65a5-4d81-9b1d-1463128fbeea","Result":[{"Value":"aaaadfgdad","SortOrder":"","ExtSysID":"","Valid":""},{"Value":"dfgdg","SortOrder":"","ExtSysID":"","Valid":""}]}],"type":"rpc","tid":6}


This is a body of the "Create" method on server side (.NET Framework):


[DirectMethod]
public DictionaryElemData Create(JObject paramsAll)
{
//paramsAll
int i = 0;
return new DictionaryElemData();
}

I can't change the type of input parameter to any my own custom type like"DictionaryElemData" with all the properties embedded inside like in the record layout on client side...

Does anybody know where is any sample how to do it. I can't find any sample using google :( !
I read some chapter in EXT.JS in Action about using DataWriter, but in my case it doesn't send any IDs at all !!!. I see parameters for read operation also here like parentID, where on book samples there were list of Ids and data objects. Why it's so weak documented and I have to spend a lot of time looking for workarounds :(

Thanks
Alex

pnrrth
5 Mar 2010, 12:39 PM
Alex,

You need to create a class in .NET that models your input data from direct. Once you have a class that models this data, direct.NET will automaticlly deserialize the data for you and hydrate your object.

for example

public class AlexResult
{
public int start {get; set;}
public int limit {get; set;}
public string sort {get; set;}
...Add your other fields in...
}

Then your Create method will look like this

[DirectMethod] public ActionReponse Create(AlexResult data) { //do stuff return this.Direct(response); }