View Full Version : EXTJS: Difference between Ext.data.Field.serialize and Ext.data.Field.convert

11 Mar 2015, 8:59 AM
What are the core differences between using convert or serialize on a model? Is there any explicit difference with these, or do they basically do the same thing. Looking at the sources (4.2.2) I didn't see any real difference at first glance. Thanx!

12 Mar 2015, 5:12 AM
Aren't they opposites? convert for data in (read), serialize for data out (write).

12 Mar 2015, 6:14 AM
actually after some testing on there implementations, it seems that they do the same thing. However; convert gets called when the proxy writer reads and writes data. Serialize only gets called when the proxy's writer goes to write data back. I would make a safe assumption that convert also gets called on any set of the model, but i didn't test this.

Knowing this, it leaves me with a new question. Why does the field not have a deserialize function that only gets called when the proxy writer reads in data?

12 Mar 2015, 7:17 AM
I think you're blurring the lines between distinct concepts.

Readers are responsible for converting raw data into models.

Writers are responsible for converting models back to raw data.

The proxy for a model will have both a reader and a writer.

A writer doesn't do any 'reading'. When a model is saved the proxy will pass the model to its writer to convert it into raw data. This is then sent to the server. The server's response will then be passed to the reader (by the proxy) so any new values for the fields can be read.

The serialize function is called by the writer during this conversion to raw data.

Historically, the convert function had the equivalent job on the reader. However, I believe that changed with ExtJS 4 and the convert function is now called whenever any data is set on the model by any means.

ExtJS 5 introduced the config option convertOnSet to disable conversion.

In ExtJS 4 I tend to use mapping instead of convert if I want conversion to happen purely in the reader. It's an abuse of mapping but it does work.

12 Mar 2015, 10:00 AM
Thanx for your info! Yes you are correct about the readers and writers, i apologize for not clearly specifying that. This is kinda what I figured was going on, but wanted to verify by someone who know a bit more about this topic.

I never thought of trying to use mapping to handle conversions, but i know what you mean its an abuse of what that was designed for.

We are migrating to version 5 soon, so I will keep that config in mind. Thanx!