PDA

View Full Version : How to parse some HTML from the HTML editor



hflipman
29 Jun 2012, 8:26 AM
Hi,

in my app I have a form that has a field of type 'htmleditor'. After the user is done editing, and hits 'save', I need to be able to look at the content of the field, and then parse and iterate over the HTML content (to convert it into a model that I can save using in a store).

How would I do that? My thought was to take the value of the field (the raw HTML from the editor), parse it as an XML object, and then iterate over the XML nodes and build the model along the way.

Any ideas?

Thanks.

Henk

scottmartin
29 Jun 2012, 3:27 PM
Here is some info about removing html from text:
http://stackoverflow.com/questions/822452/strip-html-from-text-javascript

I would recommend saving the resulting text to an object/array or json.. not XML.

Scott.

hflipman
29 Jun 2012, 10:39 PM
Thanks, Scott. I've resolved it in the following way:



var element = document.createElement("div");
element.innerHTML = values.formvalue;
parseFormElement(rec, element);


In parseElement() I iterate over the nodes and build my model. Works like a charm.

One follow-up question. 'rec' is my updated record. I then call:



rec.commit();
rec.save();


The commit() seems to work correctly since I see the data properly updated elsewhere in the app. But the save() results in a call to the server using the old data for that record.

Should I be calling something else?

Thanks.

Henk

scottmartin
30 Jun 2012, 6:13 AM
You can use record.set()



var myrec = {user: 'user', first: 'first', last: 'last'};
record.set(myrec); // if autoSync is true, then it will save, or you can call sync()


Scott.

hflipman
30 Jun 2012, 8:38 AM
Thanks, Scott!

The record gets POST-ed to the server now. But I see not all elements are there.

My model is:



Ext.define('app.model.Segment', {


extend : 'Ext.data.Model',


requires : [
'app.model.Sequence'
],


fields : [
{ name : 'id', type : 'int' }
],


hasMany : [
{
foreignKey : 'segment_id',
associationKey : 'sequences',
name : 'sequences',
model : 'app.model.Sequence'
}
]
});


When the record gets saved, I see the 'id' field in the request. But do not see the 'sequences'.

Is there an option I need to set, to make the 'save' include the 'sequences'? Sorta like a 'deep save' as opposed to the current 'shallow save'.

Thanks for all your help!

Henk

scottmartin
30 Jun 2012, 9:56 AM
There is current no support for writing to related fields at this time. Same premise us trying to update using SQL joins. You will need to update the child tables/fields accordingly as you do the parent.

Scott.

hflipman
30 Jun 2012, 7:21 PM
Hmm okay.

I'm this case the 'sequences' are a multi-valued LDAP attribute under the parent. So it really is best to have them as part of this 'save'.

Is there a quick hack to get this working?

'Load' supports it so I assumed 'save' would tool ;-).

Thanks.

Henk

hflipman
1 Jul 2012, 6:34 AM
Scott,

I've resolved this by overriding getRecordData() in Ext.data.writer.Json.

It iterates over the record's associations, and if they have stores, it calls getRecordData(). So it becomes recursive.

Thanks for your help!

Henk