PDA

View Full Version : How to insert a dirty record in EditorGrid?



ajoo
21 May 2007, 5:59 AM
First of all, thanks for the great work!

I'm building an ajax site for my wife and she's loving the cool UI a lot. :-)

One question I have about the editor grid example is:


how do I create a record and insert it into a editor grid so that it shows up as dirty (with red flag)?

I tried plant.dirty=true but it did not work.

The only way I found working for me, is to call plant.set('common', 'some new value other than new Plant 1') after plant is created by new Plant({common:'new Plant 1';...}).


But I'd like to know a more generic way so that I can create a generic function that creates dirty record for anything (not just Plant).

Ben.

ajoo
21 May 2007, 1:10 PM
A few notes:

This is regarding the EditorGrid example, where the "Add Plant" toolbar button adds a new Plant that is not dirty.


By "generic", I mean to create a insertDirtyRecord(store: Store, rec:Record) function that inserts the record into store as a dirty record.

My current solution requires the "insertDirtyRecord" function like this:


var insertDirtyRecord = function(store, rec) {
store.insert(0, rec);
rec.set('common', 'new plant');
};

Bad part of this is that it has to know the plant business of 'common' and 'new plant'.

I tried to use a loop as in:

var addDirty = function(store, rec) {
store.insert(0, rec);
for(var k in rec) {
p.set(k, rec[k]);
}
};
And call it as:

addDirty(store, new Plant({
common: 'New Plant 1',
light: 'Mostly Shade',
price: 0,
availDate: new Date(),
indoor: false
}));

Hoping that I can reuse addDirty() for all my grids. It did not work, as (I think) I'm only setting the cells to the same value as they are initialized as.


I may need quite several grids in the app and would really like to avoid having to write specific code for every different record in order to make the added record shows up as dirty.

Hope this explains better. Any suggestion or pointer is appreciated.


Ben

ajoo
22 May 2007, 12:33 PM
Well. I found a solution myself.

My addDirtyRecord was wrong. It should be:


var addDirty = function(store, type, data) {
var rec = new type({});
store.insert(0, rec);
for(var k in data) {
p.set(k, data[k]);
}
rec.modified = data;
return rec;
};

and the calling code becomes:

addDirty(store, Plant, {
common: 'New Plant 1',
light: 'Mostly Shade',
price: 0,
availDate: new Date(),
indoor: false
});

My problem is that I thought the for loop on a Record will give me all the fields of the Record. No, it doesn't. I need the record.data instead.
And the modified field (though not documented in the API reference), is very important too.


Hope this helps anyone with the same problem.