| Page 2 of 2 | [ 17 posts ] | Go to page Previous1, 2 |
Marco AsbreukYUI Contributor
|
@Sean
Even though it works, better choose a native solution. Iterating comes at the price of performance, especially when you have a bunch of items. Kind Regards, Marco. |
|
Thanks Rohit! It worked as expected.
-Bhuvana |
|
@Marco
You are correct. Will work on it today. I think I can use JSON.stringify() as satyam suggested, but only use the _oData part and ignore the rest. My RecordSets can be large (1-2K rows), so this should speed things up a bit. Sean |
|
@Marco,
I was able to use native methods by doing the following, Code: YAHOO.widget.Record.prototype.toJSON = function() { return this._oData; } and then to convert the RecordSet to a JSON string, Code: YAHOO.lang.JSON.stringify(myRecordSet.getRecords(), myWhiteList); Seems to work fine, but I'm not 100% comfortable adding the toJSON prototype method to Record. All Record instances on my page would be modified, but should be ok since the only thing I care about when converting a Record to JSON is _oData. Sean |
|
For anyone still interested,
Code: YAHOO.widget.Record.prototype.toJSON = function() { return this._oData; } should have been, Code: YAHOO.widget.Record.prototype.toJSON = function() { return this.getData(); } |
|
i didn't understand your method....
it is showing the error 'Deserilize is not defined'. Can you please post your code.... Please reply.... |
|
@masterrohitshah: You are getting that error because you are probably using YUI 3 and seanf's method was for YUI 2!
This thread is getting unnecessarily confused. Some of the responses are for DataTable YUI 2.x, and some are for YUI 3. I think this was originally posted on the YUI 3 forums, and then changed to YUI 2 ... There are differences between the versions and in how to retrieve the recordset, and data for an individual record. If you have a DataTable instance named "myDT", you can do the following; In YUI 2.x Code: var myDT = new YAHOO.widget.DataTable(....); // to get a single record's data as a JS Object { foo:'bar', baz:12345 } var recObj = myDT.getRecordSet().getRecord(7).getData(); // returns a JS Object // to return the recordset as a JS Array of JS Objects, do as seanf's method ... var RS = myDT.getRecordSet(); // returns a YAHOO RecordSet widget var jsArray = []; for (var i=0, i<RS.getLength(); i++) { jsArray.push( RS.getRecord(i).getData() ); } // to convert a JS Object into a JSON Object (i.e. JSON formatted string) JSON.stringify( recObj ); // via modern ECMAScript 5 browsers // OR, if unsure and to be safe, use YUI 2's JSON utility (normalizes across browsers) YAHOO.lang.JSON.stringify( recObj ); // both above return as follows; Example: JSON.stringify( { foo:'bar', baz:12345 }); // ... returns "{"foo":"bar","baz":12345}" // Likewise, you can use the same "stringify" on a whole JS Array ... // JSON.stringify( {mydata:jsArray} ) // ... returns "{"mydata":[ {"foo":"bar","baz":12345}, ... {"foo":"bar","baz":12345} ] }" To use the YUI 2 JSON library within your code see http://developer.yahoo.com/yui/json/. Seanf's method works in YUI 2, but you can use .stringify on the whole array, and don't have to do each row at a time. In YUI 3 (say 3.5 forwards) YUI 3 has some deprecated methods similar to YUI 2 (i.e. recordset, etc..), but they are not recommended and not fully featured. If you are using YUI 3 DT directly, you should adopt the newer non-deprecated methods. Code: var myDT = new Y.DataTable(....); // to get a single record's data as a JS Object { foo:'bar', baz:12345 } var recObj = myDT.getRecord(7).toJSON(); // returns a JS Object // to return the recordset as a JS Array of JS Objects, use ModelList's toJSON method var RS = myDT.data.toJSON(); // returns a JS Array of Objects // or var RS = myDT.get('data').toJSON(); ... identical, .data is an alias for .get('data') // to convert a JS Object into a JSON Object (i.e. JSON formatted string) JSON.stringify( recObj ); // via modern ECMAScript 5 browsers // OR, if unsure and to be safe, use YUI 3' JSON module (normalizes across browsers) Y.JSON.stringify( recObj ); // the results are identical to the code block for YUI 2 above. To use the YUI 3 JSON module include "json-stringify" in your YUI().use() loader statement. If you want to prepend something on your JSON string, you can either do the string concatenation stuff or simply defined a nested object and use .stringify to output it. Sorry this is a little long, seems like some answers and questions were getting confused regarding which version was asked about. If somebody is still unclear, a new posting might be helpful stating which version is the target 2 or 3. Todd |
| Page 2 of 2 | [ 17 posts ] | Go to page Previous1, 2 |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group