Perhaps I missed something but I don''t see an easy way to do this ? Basically just wanna hold some ui state in a cookie. Did I miss something obvious ? Best Matt *********************************************************************************** The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. Authorized and regulated by the Financial Services Authority This e-mail message is confidential and for use by the addressee only. If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer. Internet e-mails are not necessarily secure. The Royal Bank of Scotland plc does not accept responsibility for changes made to this message after it was sent. Whilst all reasonable care has been taken to avoid the transmission of viruses, it is the responsibility of the recipient to ensure that the onward transmission, opening or use of this message and any attachments will not adversely affect its systems or data. No responsibility is accepted by The Royal Bank of Scotland plc in this regard and the recipient should carry out such virus and other checks as it considers appropriate. Visit our websites at: http://www.rbos.com http://www.rbsmarkets.com ***********************************************************************************
As long as the array/object has not been touched by the prototype code (and thus would add properties to the object), the below should put you on the right track. I''m assuming you meant associative arrays as in "javascript treats all objects as associative arrays" and not the Array type object (which in that case you''d just use a prototype .each() iterator to do this)... var obj = {firstKey: firstVal, secondKey: secondVal}; var objStr = ''''; for (var p in obj) { objStr = objStr + p + ":" + obj[p] + "|"; } // ...now strip the last "|" symbol and you have your object as a string delimited by : for name/value pairs and | between the sets of values. prototype''s .flatten() might help also but I haven''t used it too much. On 4/28/06, SPENDLOVE, Matt, GBM <Matt.SPENDLOVE-ge51qsjLezM@public.gmane.org> wrote:> > Perhaps I missed something but I don''t see an easy way to do this ? > > Basically just wanna hold some ui state in a cookie. > > Did I miss something obvious ? > > Best > > Matt > > > *********************************************************************************** > The Royal Bank of Scotland plc. Registered in Scotland No 90312. > Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. > Authorized and regulated by the Financial Services Authority > > This e-mail message is confidential and for use by the > addressee only. If the message is received by anyone other > than the addressee, please return the message to the sender > by replying to it and then delete the message from your > computer. Internet e-mails are not necessarily secure. The > Royal Bank of Scotland plc does not accept responsibility for > changes made to this message after it was sent. > > Whilst all reasonable care has been taken to avoid the > transmission of viruses, it is the responsibility of the recipient to > ensure that the onward transmission, opening or use of this > message and any attachments will not adversely affect its > systems or data. No responsibility is accepted by The Royal > Bank of Scotland plc in this regard and the recipient should carry > out such virus and other checks as it considers appropriate. > Visit our websites at: > http://www.rbos.com > http://www.rbsmarkets.com > > *********************************************************************************** > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
SPENDLOVE, Matt, GBM
2006-Apr-28 14:39 UTC
RE: Serialize / deserialize an associative array
That''s pretty much what I did in the end anyway. Didn''t have any joy with flatten().. ---------------------------------------------------------------------------- ------------------------ var CradleUtils = {}; /** * These are REALLY simple serialisation tools meant for simple Hash-like objects in the for key=val */ CradleUtils.deserialize function(inStr) { return eval(''(''+inStr+'')''); } CradleUtils.serialize function(inObj) { var buf = ''{''; var cma = ''''; for (i in inObj) { buf += cma + i + " : "+inObj[i]; cma = '',''; } buf += ''}''; printfire(''serialized [''+buf+'']''); return buf; } ---------------------------------------------------------------------------- ------------------------ Example Object : var PANELS = {''a'' : true,''b'' : false,''c'' : true,''d'' : true}; ---------------------------------------------------------------------------- ------------------------ As you say, I am not realy sure of the behaviour for more complex objects. I used these to set a string value In a cookie to maintain the ui panel state.. Matt -----Original Message----- From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Ryan Gahl Sent: 28 April 2006 15:06 To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails-spinoffs] Serialize / deserialize an associative array As long as the array/object has not been touched by the prototype code (and thus would add properties to the object), the below should put you on the right track. I''m assuming you meant associative arrays as in "javascript treats all objects as associative arrays" and not the Array type object (which in that case you''d just use a prototype .each() iterator to do this)... var obj = {firstKey: firstVal, secondKey: secondVal}; var objStr = ''''; for (var p in obj) { objStr = objStr + p + ":" + obj[p] + "|"; } // ...now strip the last "|" symbol and you have your object as a string delimited by : for name/value pairs and | between the sets of values. prototype''s .flatten() might help also but I haven''t used it too much. On 4/28/06, SPENDLOVE, Matt, GBM <Matt.SPENDLOVE-ge51qsjLezM@public.gmane.org> wrote: Perhaps I missed something but I don''t see an easy way to do this ? Basically just wanna hold some ui state in a cookie. Did I miss something obvious ? Best Matt **************************************************************************** ******* The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. Authorized and regulated by the Financial Services Authority This e-mail message is confidential and for use by the addressee only. If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer. Internet e-mails are not necessarily secure. The Royal Bank of Scotland plc does not accept responsibility for changes made to this message after it was sent. Whilst all reasonable care has been taken to avoid the transmission of viruses, it is the responsibility of the recipient to ensure that the onward transmission, opening or use of this message and any attachments will not adversely affect its systems or data. No responsibility is accepted by The Royal Bank of Scotland plc in this regard and the recipient should carry out such virus and other checks as it considers appropriate. Visit our websites at: http://www.rbos.com http://www.rbsmarkets.com **************************************************************************** ******* _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On 4/28/06, SPENDLOVE, Matt, GBM <Matt.SPENDLOVE-ge51qsjLezM@public.gmane.org> wrote:> That''s pretty much what I did in the end anyway. Didn''t have any joy with > flatten()..There is also Hash.toQueryString() and String.prototype.toQueryParams(). Mind the fix from for the later, laying around in the bug tracker.> As you say, I am not realy sure of the behaviour for more complex objects.The Javascript implementation of a JSON de-/encoder by Douglas Crockford should do the trick. http://www.json.org/js.html