[Proto-Scripty] Re: Nested JSON problem
No it just means that you can only nest your data to a depth of 512 on the php server side. Imagine navigating that! On Dec 18, 7:26 pm, jason maina jason.ma...@gmail.com wrote: Is it to mean that it cant support a large 'recordset' ? On 12/18/08, Richard Quadling rquadl...@googlemail.com wrote: 2008/12/18 jason maina jason.ma...@gmail.com: Hi George, Thanks for the input. Well, the data is converted to json by php from a nested array after extracting the data from a database thus the reason why there's flowerset and flowers. Im finally through with it, it looks kinda complicated in the nesting element but it somehow while retreiving the data it was easy remebering the various layers. On the same breath will be looking into streamlining it and making it thin as you have outlined. Thanks Regards Jason On Thu, Dec 18, 2008 at 4:42 PM, George george.bea...@googlemail.com wrote: Hi Jason, Forgive me if I've misunderstood the question, but hopefully this might help. As far as I know, you can infinitely nest your JSON data if that's what you want to do. I've noticed some fundamental problems with your JSON you pasted in your post which may just be typos, but need to be cleared up before your code will work. Firstly the items in your 'flowerset' are not enclosed in square brackets. Secondly your names (eg groupCode) do not need to be enclosed in quotations although this won't break things. Thirdly, in my opinion, you could break this down and request a much smaller and less complex dataset from the server (maybe just the group names first, then depending on what the user clicked on get the flower names and so on) Thus putting less load on the server and reducing bandwidth. Also, I'd say to have everything nested under the name rsJson just adds another unnecessary dimension of complication. Think of JSON as just a long bunch of name value pairs [{name:value}] separated by commas where a value can be a number, string or another array. If your value is another array, then it must be enclosed in square brackets. As such, there should never be a curly bracket immediately after a colon ( [{name:{name:value, name:value}, name:value}] ) is wrong. [{name:[{name:value, name:value}], name:value}] is correct. I've cleaned up a little bit of your data just to demonstrate the correct syntax: {rsJson: [ {groupCode:1,groupName:Roses,flowerSet:[ {flowers: [ {flowerCode:15,flowerName:Roses} ]} ]}, {groupCode:2,groupName:Carnations,flowerSet:[ {flowers:[ {flowerCode:16,flowerName:Spray Carnations}, {flowerCode:17,flowerName:Standard Carnations} ]} ]} ]} To get to your data do something like this: myDataSetVar[0].rsJson[0].groupCode (would give you 1) myDataSetVar[0].rsJson[1].flowerSet[0].flowers[1].flowerName (would give you Standard Carnations) Really hope that helps and I haven't confused things further. All the best George On Dec 18, 9:04 am, jason maina jason.ma...@gmail.com wrote: Thanks Ryan guess was kind of tired had failed looking at some of the points rather was not getting the point. Finally I'm through this is the final product: {rsJson:[{groupCode:1,groupName:Roses,flowerSet:{flowers:[{flowerCode:15,flowerName:Roses}]}},{groupCode:2,groupName:Carnations,flowerSet:{flowers:[{flowerCode:16,flowerName:Spray Carnations},{flowerCode:17,flowerName:Standard Carnations}]}},{groupCode:3,groupName:Perennials,flowerSet:{flowers:[{flowerCode:2,flowerName:Aster},{flowerCode:3,flowerName:Alstromeria},{flowerCode:10,flowerName:Gysophila},{flowerCode:12,flowerName:Limonium Perezzi}]}},{groupCode:4,groupName:Annuals,flowerSet:{flowers:[{flowerCode:1,flowerName:Ammi Majus},{flowerCode:4,flowerName:Bupleurum},{flowerCode:5,flowerName:Carthamus},{flowerCode:6,flowerName:Chinese Aster},{flowerCode:7,flowerName:Chrysantemum},{flowerCode:8,flowerName:Delphinium},{flowerCode:13,flowerName:Lisianthus},{flowerCode:14,flowerName:Molucella},{flowerCode:18,flowerName:Statice},{flowerCode:19,flowerName:Stocks}]}},{groupCode:5,groupName:Bulbs,flowerSet:{flowers:[{flowerCode:9,flowerName:Freesia},{flowerCode:11,flowerName:Lilies}]}}]} kind regards Jason On Wed, Dec 17, 2008 at 8:04 PM, jason maina jason.ma...@gmail.com wrote: What im wondering out of the reply is I cant have a nested JSON object with the following layout? groupID, groupName, groupItems(array of items) similar example: http://labs.adobe.com/technologies/spry/samples/data_region/JSONDataS... Regards, Jason On Wed, Dec 17, 2008 at 7:02 PM, Ryan Gahl ryan.g...@gmail.com wrote: typo... ...with NO properties On Wed, Dec 17, 2008 at 10:02 AM, Ryan Gahl ryan.g...@gmail.com wrote: Here are your problems... I'll remove the bulk of the data to make
[Proto-Scripty] Re: Sorting on a JSON array
Just wondering if anyone had any ideas about how to sort JSON data. I'm sure there must be an easy way. Many thanks George On Dec 16, 12:43 pm, George george.bea...@googlemail.com wrote: Hi Folks, I wondered if there was a way to sort a JSON array. Here's an example: var RS = [ {id: 1, dateFrom: 20090101, dateTo: 20090309, tariff: [ {guest: 1, mon: 105, tue: 79, wed: 79, thu: 79, fri: 99, sat: 109, sun: 79}, {guest: 2, mon: 89, tue: 89, wed: 89, thu: 89, fri: 109, sat: 119, sun: 89} ]}, {id: 2, dateFrom: 20090302, dateTo: 20090304, tariffhigh: 180, tarifflow: 150, tariff: [ {guest: 1, mon: 240, tue: 0, wed: 0, thu: 0, fri: 0, sat: 0, sun: 0}, {guest: 2, mon: 220, tue: 0, wed: 0, thu: 0, fri: 0, sat: 0, sun: 0} ]}, {id: 0, dateFrom: 20081216, dateTo: 20081216, tariff: [ {guest: 1, mon: 0, tue: 0, wed: 0, thu: 0, fri: 0, sat: 0, sun: 0} ]} ] RS.sortBy(function(i){return i.dateFrom},this) I've simplified this code slightly but you should get the general idea. I'm trying to reorder by dateFrom but I can't get sortBy to sort in this case. Anyone know what I'm doing wrong, or is there an altogether better way? Many thanks George --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Nested JSON problem
Hi Jason, Forgive me if I've misunderstood the question, but hopefully this might help. As far as I know, you can infinitely nest your JSON data if that's what you want to do. I've noticed some fundamental problems with your JSON you pasted in your post which may just be typos, but need to be cleared up before your code will work. Firstly the items in your 'flowerset' are not enclosed in square brackets. Secondly your names (eg groupCode) do not need to be enclosed in quotations although this won't break things. Thirdly, in my opinion, you could break this down and request a much smaller and less complex dataset from the server (maybe just the group names first, then depending on what the user clicked on get the flower names and so on) Thus putting less load on the server and reducing bandwidth. Also, I'd say to have everything nested under the name rsJson just adds another unnecessary dimension of complication. Think of JSON as just a long bunch of name value pairs [{name:value}] separated by commas where a value can be a number, string or another array. If your value is another array, then it must be enclosed in square brackets. As such, there should never be a curly bracket immediately after a colon ( [{name:{name:value, name:value}, name:value}] ) is wrong. [{name:[{name:value, name:value}], name:value}] is correct. I've cleaned up a little bit of your data just to demonstrate the correct syntax: {rsJson: [ {groupCode:1,groupName:Roses,flowerSet:[ {flowers: [ {flowerCode:15,flowerName:Roses} ]} ]}, {groupCode:2,groupName:Carnations,flowerSet:[ {flowers:[ {flowerCode:16,flowerName:Spray Carnations}, {flowerCode:17,flowerName:Standard Carnations} ]} ]} ]} To get to your data do something like this: myDataSetVar[0].rsJson[0].groupCode (would give you 1) myDataSetVar[0].rsJson[1].flowerSet[0].flowers[1].flowerName (would give you Standard Carnations) Really hope that helps and I haven't confused things further. All the best George On Dec 18, 9:04 am, jason maina jason.ma...@gmail.com wrote: Thanks Ryan guess was kind of tired had failed looking at some of the points rather was not getting the point. Finally I'm through this is the final product: {rsJson:[{groupCode:1,groupName:Roses,flowerSet:{flowers:[{flowerCode:15,flowerName:Roses}]}},{groupCode:2,groupName:Carnations,flowerSet:{flowers:[{flowerCode:16,flowerName:Spray Carnations},{flowerCode:17,flowerName:Standard Carnations}]}},{groupCode:3,groupName:Perennials,flowerSet:{flowers:[{flowerCode:2,flowerName:Aster},{flowerCode:3,flowerName:Alstromeria},{flowerCode:10,flowerName:Gysophila},{flowerCode:12,flowerName:Limonium Perezzi}]}},{groupCode:4,groupName:Annuals,flowerSet:{flowers:[{flowerCode:1,flowerName:Ammi Majus},{flowerCode:4,flowerName:Bupleurum},{flowerCode:5,flowerName:Carthamus},{flowerCode:6,flowerName:Chinese Aster},{flowerCode:7,flowerName:Chrysantemum},{flowerCode:8,flowerName:Delphinium},{flowerCode:13,flowerName:Lisianthus},{flowerCode:14,flowerName:Molucella},{flowerCode:18,flowerName:Statice},{flowerCode:19,flowerName:Stocks}]}},{groupCode:5,groupName:Bulbs,flowerSet:{flowers:[{flowerCode:9,flowerName:Freesia},{flowerCode:11,flowerName:Lilies}]}}]} kind regards Jason On Wed, Dec 17, 2008 at 8:04 PM, jason maina jason.ma...@gmail.com wrote: What im wondering out of the reply is I cant have a nested JSON object with the following layout? groupID, groupName, groupItems(array of items) similar example: http://labs.adobe.com/technologies/spry/samples/data_region/JSONDataS... Regards, Jason On Wed, Dec 17, 2008 at 7:02 PM, Ryan Gahl ryan.g...@gmail.com wrote: typo... ...with NO properties On Wed, Dec 17, 2008 at 10:02 AM, Ryan Gahl ryan.g...@gmail.com wrote: Here are your problems... I'll remove the bulk of the data to make it more clear: rsJson: {[{...}]} what you have is illegal. You're essentially saying you have an anonymous object with not properties and filling it with an array (that's probably not even totally accurate)... the bottom line is, you can't have a curly brace immediately followed by a square brace. And going further you have it all just sitting in a property floating in space. The first thing you need to do is wrap the property in curlies so you're actually returning an object... {rsJson: ...} Now, this other issue of putting a square bracket right inside a curly... You can either a) remove the outer curlies and just make rsJson be an array, or b) add a property name which then becomes the array. a) {rsJson: [{}, {}, ...]} b) {rsJson: {myArray: [...]}} On Wed, Dec 17, 2008 at 9:53 AM, jason maina jason.ma...@gmail.comwrote: Hi all, Below is JSON encoded(php) data. After evaluating and sanitizing it im not capable of extracting its contents im just not sure what im doing wrong: [php output] rsJson:{[{groupCode:1
[Proto-Scripty] Re: Nested JSON problem
You learn something every day! Many thanks for that Richard. On Dec 18, 4:16 pm, Richard Quadling rquadl...@googlemail.com wrote: 2008/12/18 jason maina jason.ma...@gmail.com: Hi George, Thanks for the input. Well, the data is converted to json by php from a nested array after extracting the data from a database thus the reason why there's flowerset and flowers. Im finally through with it, it looks kinda complicated in the nesting element but it somehow while retreiving the data it was easy remebering the various layers. On the same breath will be looking into streamlining it and making it thin as you have outlined. Thanks Regards Jason On Thu, Dec 18, 2008 at 4:42 PM, George george.bea...@googlemail.com wrote: Hi Jason, Forgive me if I've misunderstood the question, but hopefully this might help. As far as I know, you can infinitely nest your JSON data if that's what you want to do. I've noticed some fundamental problems with your JSON you pasted in your post which may just be typos, but need to be cleared up before your code will work. Firstly the items in your 'flowerset' are not enclosed in square brackets. Secondly your names (eg groupCode) do not need to be enclosed in quotations although this won't break things. Thirdly, in my opinion, you could break this down and request a much smaller and less complex dataset from the server (maybe just the group names first, then depending on what the user clicked on get the flower names and so on) Thus putting less load on the server and reducing bandwidth. Also, I'd say to have everything nested under the name rsJson just adds another unnecessary dimension of complication. Think of JSON as just a long bunch of name value pairs [{name:value}] separated by commas where a value can be a number, string or another array. If your value is another array, then it must be enclosed in square brackets. As such, there should never be a curly bracket immediately after a colon ( [{name:{name:value, name:value}, name:value}] ) is wrong. [{name:[{name:value, name:value}], name:value}] is correct. I've cleaned up a little bit of your data just to demonstrate the correct syntax: {rsJson: [ {groupCode:1,groupName:Roses,flowerSet:[ {flowers: [ {flowerCode:15,flowerName:Roses} ]} ]}, {groupCode:2,groupName:Carnations,flowerSet:[ {flowers:[ {flowerCode:16,flowerName:Spray Carnations}, {flowerCode:17,flowerName:Standard Carnations} ]} ]} ]} To get to your data do something like this: myDataSetVar[0].rsJson[0].groupCode (would give you 1) myDataSetVar[0].rsJson[1].flowerSet[0].flowers[1].flowerName (would give you Standard Carnations) Really hope that helps and I haven't confused things further. All the best George On Dec 18, 9:04 am, jason maina jason.ma...@gmail.com wrote: Thanks Ryan guess was kind of tired had failed looking at some of the points rather was not getting the point. Finally I'm through this is the final product: {rsJson:[{groupCode:1,groupName:Roses,flowerSet:{flowers:[{flowerCode:15,flowerName:Roses}]}},{groupCode:2,groupName:Carnations,flowerSet:{flowers:[{flowerCode:16,flowerName:Spray Carnations},{flowerCode:17,flowerName:Standard Carnations}]}},{groupCode:3,groupName:Perennials,flowerSet:{flowers:[{flowerCode:2,flowerName:Aster},{flowerCode:3,flowerName:Alstromeria},{flowerCode:10,flowerName:Gysophila},{flowerCode:12,flowerName:Limonium Perezzi}]}},{groupCode:4,groupName:Annuals,flowerSet:{flowers:[{flowerCode:1,flowerName:Ammi Majus},{flowerCode:4,flowerName:Bupleurum},{flowerCode:5,flowerName:Carthamus},{flowerCode:6,flowerName:Chinese Aster},{flowerCode:7,flowerName:Chrysantemum},{flowerCode:8,flowerName:Delphinium},{flowerCode:13,flowerName:Lisianthus},{flowerCode:14,flowerName:Molucella},{flowerCode:18,flowerName:Statice},{flowerCode:19,flowerName:Stocks}]}},{groupCode:5,groupName:Bulbs,flowerSet:{flowers:[{flowerCode:9,flowerName:Freesia},{flowerCode:11,flowerName:Lilies}]}}]} kind regards Jason On Wed, Dec 17, 2008 at 8:04 PM, jason maina jason.ma...@gmail.com wrote: What im wondering out of the reply is I cant have a nested JSON object with the following layout? groupID, groupName, groupItems(array of items) similar example: http://labs.adobe.com/technologies/spry/samples/data_region/JSONDataS... Regards, Jason On Wed, Dec 17, 2008 at 7:02 PM, Ryan Gahl ryan.g...@gmail.com wrote: typo... ...with NO properties On Wed, Dec 17, 2008 at 10:02 AM, Ryan Gahl ryan.g...@gmail.com wrote: Here are your problems... I'll remove the bulk of the data to make it more clear: rsJson: {[{...}]} what you have is illegal. You're essentially saying you have an anonymous object with not properties and filling
[Proto-Scripty] Re: Ajax onComplete, but from another function
Hi TJ Many thanks for your response. Actualy I'm not having any problem with execution scope and have managed to grasp a fairly good understanding over the past few weeks. In my constant attempts to re-use code, I'm trying to revisit a method that refreshes a dataset with an ajax call. The problem that I'm running into though is that I'm not able to determine when the data has been refreshed successfully from outside the method. This results in my calling function charging on and refreshing my page with out of date data. So my question is - is there some kind of global status I can access to tell whether or not the Ajax call has successfully completed. All the best George On Dec 15, 5:56 pm, T.J. Crowder t...@crowdersoftware.com wrote: Hi George, If I'm reading that right on a fairly quick skim, it looks like you've already taken a stab at this and gotten very close: onSuccess : function(transport){ this.RS = transport.responseText.evalJSON(); if (a==1) this._drawPage; });}, The thing is that this, within the onSuccess handler, won't be what it is in the _getData call. this is set by how a function is called, rather than where and how it's defined. The anonymous function being used for the onSuccess handler will be called with this being something else (I don't immediately recall what), rather than it being a reference to an instance of your class. This is easily fixed, though, using Function#bind[1]: onSuccess : (function(transport){ this.RS = transport.responseText.evalJSON(); if (a==1) this._drawPage(); }).bind(this));}, [1]http://prototypejs.org/api/function/bind I put parens around the function definition, and then put .bind(this) after it. I also put parens after the this._getData call to make it a call to the function rather than a reference to the function object. Details on the Function#bind page, an also here are some useful articles: http://proto-scripty.wikidot.com/prototype:how-to-hooking-events#Boun...http://blog.niftysnippets.org/2008/04/you-must-remember-this.htmlhttp://www.alistapart.com/articles/getoutbindingsituations (I'm rushing a bit this morning, apologies if I misread the question or code and the above is off-base.) HTH, -- T.J. Crowder tj / crowder software / com On Dec 15, 5:07 am, George george.bea...@googlemail.com wrote: Hi Folks, I'm wondering if there is a way to get a function to wait until an external Ajax.Request has completed. As always, I find an example is the best way to describe my issue: var buildPage = {}; buildPage = Class.create(); buildPage.prototype = { initialize: function() { this.RS = [];//hold json data for later use ... this._getData(1);}, _getData : function(a) { var url = (a==1) ? 'url1' : 'url2'; new Ajax.Request (url), {method:'get', onSuccess : function(transport){ this.RS = transport.responseText.evalJSON(); if (a==1) this._drawPage; });}, _drawPage : function () { //parse this.RS data with template ...}, _refreshData : function () { this.getData(2); //wait until _getData reports success then continue this._drawPage(); } }; So this buildPage class reuses the _getData method calling a different URL depending on what is passed to it. What I want the _refreshData method to do is call the _getData method, and only when it has successfully refreshed the RS array, call the _drawPage method. Is this possible? should I be doing it a different way? Many thanks George --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Extracting the name from a JSON object
Hi T.J. I'm very grateful for this straight forward and easy to follow explanation - it's certainly broadened my whole understanding of JavaScript arrays and object literals - and helped me write a concise solution to the JavaScript problem I was facing. All the best George On Dec 5, 1:37 pm, T.J. Crowder [EMAIL PROTECTED] wrote: Hi George, (It's a JavaScript rather than Prototype/script.aculo.us question, yes.) This object literal / JSON data: [{bookingref:'A6D98FGR', canceled:0}] ...defines an array with a single element, which is an object instance with two properties: bookingref (value 'A6D98FGR') and canceled (value 0). You can get the values just by referring to the properties of the object, so: var x = RS[0].bookingref; alert(x); // Alerts 'A6D98FGR' JavaScript allows you to use property names both literally with dot notation (as above), and _also_ via string names using bracket notation; we could write the above like this instead: var x = RS[0]['bookingref']; alert(x); // Alerts 'A6D98FGR' Note the quotes, the square brackets, and the absense of the dot. If you don't know the names of the properties in advance, you can use the for..in loop to iterate over the names of the object's properties: var name; for (name in RS[0]) { alert(name + '=' + RS[0][name]); } In the loop, the variable 'name' is set on each iteration to the name of a property on the object, as a string. This is powerful when combined with bracket notation. On the object defined in your JSON above, that will show bookingref=A6D98FGR and canceled=0; the order is not defined and almost certainly will vary from implementation to implementation. Note that for..in is for iterating over the properties of an object, *not* the elements of an array. Many JavaScript programmers think it's for the latter, and they get into trouble as a result because Prototype adds some properties to arrays that they're not expecting to see. Details:http://proto-scripty.wikidot.com/prototype:tip-looping-through-arrays But again, it's totally fine for looping through the properties on an object, like your RS[0]. HTH, -- T.J. Crowder tj / crowder software / com On Dec 5, 12:18 pm, George [EMAIL PROTECTED] wrote: Hi Folks, This may be more of a pure JavaScript question than Prototype, but here goes: If I have a JSON array called RS for example containing this: [{bookingref:'A6D98FGR', canceled:0}] is there a way for me to programatically get the names and values? I'd like to be able to do something like RS[0].[0].name ((would be 'bookingref')) RS[0].[0].value ((would be 'A6D98FGR')) I hope that makes sense. Many thanks George --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Extracting the name from a JSON object
Hi Gabriel - many thanks for this tip! All the best George On Dec 5, 10:04 pm, Gabriel Gilini [EMAIL PROTECTED] wrote: I forgot to say that apparently Safari = 2.0 and IE5.2 on mac doesn't implement the hasOwnProperty method. Gabriel Gilini www.usosim.com.br [EMAIL PROTECTED] [EMAIL PROTECTED] On Fri, Dec 5, 2008 at 8:01 PM, Gabriel Gilini [EMAIL PROTECTED]wrote: If you want to iterate through an array with the constructor object's prototype extended use Object.prototype.hasOwnProperty(). var arr = [0,1,2,'foo','bar']; for(var i in arr){ if(arr.hasOwnProperty){ if(arr.hasOwnProperty(i)) alert(arr[i]); } } Gabriel Gilini www.usosim.com.br [EMAIL PROTECTED] [EMAIL PROTECTED] On Fri, Dec 5, 2008 at 11:37 AM, T.J. Crowder [EMAIL PROTECTED]wrote: Hi George, (It's a JavaScript rather than Prototype/script.aculo.us question, yes.) This object literal / JSON data: [{bookingref:'A6D98FGR', canceled:0}] ...defines an array with a single element, which is an object instance with two properties: bookingref (value 'A6D98FGR') and canceled (value 0). You can get the values just by referring to the properties of the object, so: var x = RS[0].bookingref; alert(x); // Alerts 'A6D98FGR' JavaScript allows you to use property names both literally with dot notation (as above), and _also_ via string names using bracket notation; we could write the above like this instead: var x = RS[0]['bookingref']; alert(x); // Alerts 'A6D98FGR' Note the quotes, the square brackets, and the absense of the dot. If you don't know the names of the properties in advance, you can use the for..in loop to iterate over the names of the object's properties: var name; for (name in RS[0]) { alert(name + '=' + RS[0][name]); } In the loop, the variable 'name' is set on each iteration to the name of a property on the object, as a string. This is powerful when combined with bracket notation. On the object defined in your JSON above, that will show bookingref=A6D98FGR and canceled=0; the order is not defined and almost certainly will vary from implementation to implementation. Note that for..in is for iterating over the properties of an object, *not* the elements of an array. Many JavaScript programmers think it's for the latter, and they get into trouble as a result because Prototype adds some properties to arrays that they're not expecting to see. Details: http://proto-scripty.wikidot.com/prototype:tip-looping-through-arrays But again, it's totally fine for looping through the properties on an object, like your RS[0]. HTH, -- T.J. Crowder tj / crowder software / com On Dec 5, 12:18 pm, George [EMAIL PROTECTED] wrote: Hi Folks, This may be more of a pure JavaScript question than Prototype, but here goes: If I have a JSON array called RS for example containing this: [{bookingref:'A6D98FGR', canceled:0}] is there a way for me to programatically get the names and values? I'd like to be able to do something like RS[0].[0].name ((would be 'bookingref')) RS[0].[0].value ((would be 'A6D98FGR')) I hope that makes sense. Many thanks George --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Extracting the name from a JSON object
Hi Folks, This may be more of a pure JavaScript question than Prototype, but here goes: If I have a JSON array called RS for example containing this: [{bookingref:'A6D98FGR', canceled:0}] is there a way for me to programatically get the names and values? I'd like to be able to do something like RS[0].[0].name ((would be 'bookingref')) RS[0].[0].value ((would be 'A6D98FGR')) I hope that makes sense. Many thanks George --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Can't call a function within a class using each or invoke
T.J. - Mona, Thank you both very much for your solutions, recommended reading lists and T.J, for your event delegation suggestion. FYI Mona's solution worked out of the box but T.J. your event delegation suggestion makes a lot of sense - I suspect I will add a new method to my class to handle all clicks. I'm going to do some reading first though - will start with ALA's article on binding. Many thanks George On Nov 17, 1:54 pm, T.J. Crowder [EMAIL PROTECTED] wrote: Doh! I missed he was doing all of this in an invoke, what I get for skimming. Wow, that's convoluted. Good answer, though. George, AFAICS Mona's solution will work for your _editField function as defined. But FWIW, I'd probably define a separate function intended as an event handler. (The reason Mona's had to create an anonymous function to bind is that she needs to ditch the event object parameter _editField isn't expecting to see.) Doing that would simplify things a bit, I think, and make it easier for debugging. Another thing you might consider is event delegation: Hook the 'click' event on whatever the container of these 'editable's is, and use one event handler rather than many. Clicks on descendent elements bubble up to the parent container, and you can get the element on which the click actually happened from the event object. So given this, for instance: div id='container' span class='editable'foo foo foo/span span class='editable'bar bar barspan /div You can just hook 'click' on the 'container' rather than the editables: $('container').observe('click', this.handleContainerClick.bind(this)); and your handler might look like this: function handleContainerClick(event) { var elm; // Get the element on which the click actually occurred elm = event.findElement(); // Does it exist, and is it editable? if (elm elm.hasClassName('editable')) { // Yes, edit it this._editField(elm); } } Various links on event delegation can be found here:http://proto-scripty.wikidot.com/faq#delegation HTH, -- T.J. Crowder tj / crowder software / com On Nov 17, 1:34 pm, Mona Remlawi [EMAIL PROTECTED] wrote: great readings suggested. when you finish, you can try the following :) $$('.editable').each(function(e) {e.observe('click', (function() {this._editField(e)}).bind(this))}, this); the problem is that when you iterate, you have to specify the context. On Mon, Nov 17, 2008 at 2:15 PM, T.J. Crowder [EMAIL PROTECTED] wrote: Hi George, Go back to school! ;-) No, seriously, you don't need to wrap the call to bindAsEventListener () in a function; that's its job. You also don't have to pass the event object to it. (And you probably want bind()[1] rather than bindAsEventListener().) [1]http://prototypejs.org/api/function/bind Some resources that will probably help, both with the concept... http://blog.niftysnippets.org/2008/04/you-must-remember-this.html http://www.alistapart.com/articles/getoutbindingsituations ...and the details: http://proto-scripty.wikidot.com/prototype:how-to-hooking-events (the example near the end alks about instance methods, but I recommend reading through from the beginning). HTH, -- T.J. Crowder tj / crowder software / com On Nov 17, 1:00 pm, George [EMAIL PROTECTED] wrote: Hi Folks, I'm pretty sure that I'm going to be told to go back to school here, but I've wasted a whole morning trying to figure this out - now it's time to ask the experts. I've simplified my code here to demonstrate what I'm trying to do: [CODE] var buildPage = {}; buildPage = Class.create(); buildPage.prototype = { initialize: function() { this.rowTemplate = 'tr...' this.rs = []; this._getData(); },// initialize _getData : function() {//using AJAX to grab a JSON array from the server and store it in this.rs}, _parseTemplate : function() {//write data to screen then add event listeners $$('.editable').invoke('observe', 'mouseover',function(e){e.element ().addClassName('pseudo-text-box')}); $$('.editable').invoke('observe', 'mouseout',function(e){e.element ().removeClassName('pseudo-text-box')}); $$('.editable').invoke('observe', 'click', function(e) {this._editField.bindAsEventListener(this, e) }); }, _editField : function(e) { //Inject INPUT field} }; [/CODE] My problem is with this line: $$('.editable').invoke('observe', 'click', function(e) {this._editField.bindAsEventListener(this, e) }); At runtime, I always get the message that this._editField is not a function, no matter how I try the binding. The only way I can get this to work is to initiate the class (var a = new buildPage) than call c._editField from within the function - but that's not much good when I come to re-use it. Can someone explain to me what's going on here