PDA

View Full Version : Creating a dynamic Record Object



kaps77
22 Aug 2007, 9:57 PM
Hi,
I need to add a new Record in a grid using these 2 steps:

var TopicRecord = Ext.data.Record.create(
{name: 'title', mapping: 'topic_title'},
{name: 'author', mapping: 'username'},
{name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
{name: 'lastPost', mapping: 'post_time', type: 'date'},
{name: 'lastPoster', mapping: 'user2'},
{name: 'excerpt', mapping: 'post_text'}
);



var myNewRecord = new TopicRecord({
title: 'Do my job please',
author: 'noobie',
totalPosts: 1,
lastPost: new Date(),
lastPoster: 'Animal',
excerpt: 'No way dude!'
});
myStore.add(myNewRecord);


In the second step i need to extract the keys for the Object TopicRecord from the grid itself using:ColumnModel.getColumnId().
(Am doing this because the grid that i create consumes a 3rd party xml which provides for dynamic columns)
Since my keys are dynamic i cannot create the TopicRecord Object in the way it is mentioned in the example.
Can i create an Object of TopicRecord with dynamic keys?
If so, can someone please post a code snippet that enables me to do so?

Thanks a lot!

Animal
22 Aug 2007, 11:16 PM
I don't understand anything that you just said.

"extract the keysfor the Object TopicRecord"???

kaps77
23 Aug 2007, 12:53 AM
The object TopicRecord is created using keys and values.
In the example snippet

var myNewRecord = new TopicRecord({
title: 'Do my job please'
});
I refer to the 'title' as key (like the map notation in java).
and value is 'Do my job please'

My problem is that since even the keys are dynamic, how do i create a TopicRecord object.
I hope this sheds some clarity on the issue.
Thanks in advance

Animal
23 Aug 2007, 1:20 AM
Your record's prototype contains a MixedCollection called "fields" which contains Fields which have a "name" property.

But I still don't get what you want to do. If you are writing code to create a Record, won't you know the field names?

Otherwise how are you writing that code if you don't know what you are creating?

Please post some context.

kaps77
23 Aug 2007, 4:54 AM
Hi Animal,
The Context is as follows:

I have a grid which allows the user to configure the columns, when i say configure i mean not show / hide as is the normal behaviour with the Grid but parsing an XML (received from server) that gives me the:
1. Number of columns
2. Column Id
3. Column Headers
4. The Data records

This means that the grid may have "n" number of columns, depending on the columns that the user wants to see.

Now I also receive an xml that gives me the new record that i need to insert into the grid.
This Xml contains the following data:
1. Column Id
2. Column Data

I parse this xml to get the list of Column Ids in form of an array (or whatever data structure suits you).
Now comes the part where i need to create a record object to insert into the grid. The Record object requires me to know the keys(see explaination for what i call keys, above). In this case the keys are the ColumnId that i receive in the XML above.
Hence the question, how do i create a Record Object with the keys that i derive from the xml received above.

Thanks for being so patient, if there is still some doubt as to the usage please feel free to ask me.

Animal
23 Aug 2007, 5:03 AM
You just told me you have the column id. You highlighted it in red, so where's the problem in creating a Record's data object?

kaps77
23 Aug 2007, 5:49 AM
Hi,
The problem is with this code:

var myNewRecord = new TopicRecord({
title: 'Do my job please'
});

Am receving the columnId 'title' as a attribute from XML, for example the XML is in the form of
<attribute id='title' value = 'Do my jpb please '/>

I process this xml and convert id and value into two javascript variable say 'id' and 'val'
Now am doing something like this

var myNewRecord = new TopicRecord({
id: val
});
myStore.add(myNewRecord);

Here both the Id and value are variables. Nothing happens when this code is executed.

Animal
23 Aug 2007, 6:09 AM
var newRecData = {};
newRecData[id] = val;
var myNewRecord = new TopicRecord(newRecData);
myStore.add(myNewRecord);

kaps77
23 Aug 2007, 6:35 AM
Thanks man, this works!