Deleting ExtensionData From JavaScript
WCF DataContracts include an ExtensionData property which is a bit troublesome if you are trying to send a modified object back up to the service if it has no properties in Javascript.
So I wrote a little piece of Javascript to clean it up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<FONT face="Lucida Console"><FONT color=#4d70ea>function </FONT><FONT color=#e87400>DeleteExtensionData(obj) {</FONT> <FONT color=#4d70ea>var </FONT><FONT color=#e87400>keys = Object.keys(obj);</FONT> <FONT color=#e87400>keys.each(</FONT><FONT color=#4d70ea>function</FONT><FONT color=#e87400>(key) {</FONT> <FONT color=#4d70ea>if</FONT><FONT color=#e87400>(!Object.isFunction(obj[key])) {</FONT> <FONT color=#4d70ea>if</FONT><FONT color=#e87400>(obj[key] </FONT><FONT color=#4d70ea>instanceof </FONT><FONT color=#e87400>Object) {</FONT> <FONT color=#e87400>DeleteExtensionData(obj[key]);</FONT> <FONT color=#e87400>}</FONT> <FONT color=#4d70ea>if</FONT><FONT color=#e87400>(key == </FONT><FONT color=#a56dbc>"ExtensionData"</FONT><FONT color=#e87400>) {</FONT> <FONT color=#4d70ea>delete </FONT><FONT color=#e87400>obj[key];</FONT> <FONT color=#e87400>}</FONT> <FONT color=#e87400>}</FONT> <FONT color=#e87400>});</FONT> <FONT color=#e87400>}</FONT></FONT> |
It will recusively delete all ExtensionData properties from the object. You can call as soon as you get the result from a completed AJAX request or you can call before sending an object parameter to a service.
Note that it uses constructs from prototype.
If you want to get fancy, you can also write a custom serializer.