[Proto-Scripty] Re: Objects that return zero length
On Jul 15, 11:24 pm, Ron Newman ron.new...@gmail.com wrote: Guinness or Stout? Guinness is a stout, --~--~-~--~~~---~--~~ 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: Objects that return zero length
Hmm, well you'd have to do a little extra filtering but no reason this isn't possible. assuming obj is your main structure you just posted do something like this... var edgeArr = $H(obj.nodes).keys().findAll(function(itr){ return ! (obj.nodes[itr].edges instanceof Array);}).collect(function(key) { return obj.nodes[key]; }); edgeArr is now an array of all objects that have a valid edges structure. ps. You owe me one pint of beer. On Jul 14, 10:47 pm, ronman ron.new...@gmail.com wrote: Yes, that's pretty close. attr is an object with properties, and one of those, attr.edges, is a nested object with its own nested sub- objects. All these properties and objects are constructed by evalJson () and then put in a $H() in a manner very similar to what you just described. I've copied a sample of the structure below. I was assuming that putting this whole thing within $A() would make the individual members (properties and objects) iterable, but perhaps I'm misunderstanding and instead every property and sub-object must be iterable (that is, understand the [] operator and have a length() method). Unfortunately, when the sub-object edges is empty, then running each () on the hash that contains it fails. arr.each(function(edgeProperty){ // fails if arr is an empty Hash. works if arr is an empty Array. fails if arr is a filled Array. Notice that the first edges is empty and the second one has one member: Brainwriting. from json: { nodes: { Brain Writing : { label: BRAIN\\nWRITING , height: 0.50, width: 0.83, fontsize: 10.00, fontcolor: white, pos: [ 570, 213 ], id: 1695, onmousedown: startdrag('1695'), length: 1, edges: [ ] }, A Perfect Brainstorm : { label: A PERFECT\\nBRAINSTORM\\n, height: 0.50, width: 1.11, fontsize: 10.00, fontcolor: white, pos: [ 326, 167 ], class: node, onmousedown: startdrag('1701'), length: 1, edges: { Brainwriting : { color: black, penwidth: 0.5, fontname: Arial, URL: javascript:void(predwin(2179)), label: bull; , } Ron blog.ideatree.us On Jul 14, 4:47 pm, Matt Foster mattfoste...@gmail.com wrote: Oh I get it now, attr.edges isn't an array of edge objects, its just an object with a series of properties that define an edge to your shape? If that is the case and you wish to iterate over the properties and execute set on them explicitly, you could do something like... var arr = $H(attr.edges).keys(); arr.each(function(edgeProperty){ this[edgeProperty].set(...); }, attr.edges); On Jul 14, 3:09 pm, ronman ron.new...@gmail.com wrote: Here's what I'm doing now: $A(attr.edges).each(function(edge) { edge.set(id, new GraphEdge(cv, id, edge)); }, this); Same result. The iterator.length shows zero and the inside of the loop never executes. But the iterator is (correctly, I think) 'Object Brainwriting =Object' Just casting around, I also tried taking off the binding to 'this' at the end, doesn't help. As before, I'm not getting why anything whatsoever that's in an array shouldn't automatically return length 0. On Jul 14, 1:47 pm, Matt Foster mattfoste...@gmail.com wrote: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); edge is your variable, why bother trying to set function ownership? The function is sent the parameter it needs to act upon. Just remove this from your this.edge... line and you should be good. -- http://positionabsolute.net On Jul 14, 2:28 pm, ronman ron.new...@gmail.com wrote: This is a newbie question, but I learn a lot every time I ask. Why do some objects return a zero length? I'm doing Array.each. In the array at this particular time is a single object. Firebug describes that object as Object Brainwriting color=black penwidth=0.5 fontname=Arial and I iterate through the array this way: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); But in Prototype's each method the 'edge' iterator (the Brainwriting Object), returns zero length: function $A(iterable) { if (!iterable) return []; if (iterable.toArray) return iterable.toArray(); var length = iterable.length || 0, results = new Array(length); // LENGTH NOW IS ZERO while (length--) results[length] = iterable[length]; return results; } Maybe the Brainwriting Object is not iterable, but I would expect $A (attr.edges) to have made it so before my call to each(). Surely I'm not expected to assign a length attribute to objects myself - just
[Proto-Scripty] Re: Objects that return zero length
Guinness or Stout? On Wed, Jul 15, 2009 at 4:58 PM, Matt Foster mattfoste...@gmail.com wrote: Hmm, well you'd have to do a little extra filtering but no reason this isn't possible. assuming obj is your main structure you just posted do something like this... var edgeArr = $H(obj.nodes).keys().findAll(function(itr){ return ! (obj.nodes[itr].edges instanceof Array);}).collect(function(key) { return obj.nodes[key]; }); edgeArr is now an array of all objects that have a valid edges structure. ps. You owe me one pint of beer. On Jul 14, 10:47 pm, ronman ron.new...@gmail.com wrote: Yes, that's pretty close. attr is an object with properties, and one of those, attr.edges, is a nested object with its own nested sub- objects. All these properties and objects are constructed by evalJson () and then put in a $H() in a manner very similar to what you just described. I've copied a sample of the structure below. I was assuming that putting this whole thing within $A() would make the individual members (properties and objects) iterable, but perhaps I'm misunderstanding and instead every property and sub-object must be iterable (that is, understand the [] operator and have a length() method). Unfortunately, when the sub-object edges is empty, then running each () on the hash that contains it fails. arr.each(function(edgeProperty){ // fails if arr is an empty Hash. works if arr is an empty Array. fails if arr is a filled Array. Notice that the first edges is empty and the second one has one member: Brainwriting. from json: { nodes: { Brain Writing : { label: BRAIN\\nWRITING , height: 0.50, width: 0.83, fontsize: 10.00, fontcolor: white, pos: [ 570, 213 ], id: 1695, onmousedown: startdrag('1695'), length: 1, edges: [ ] }, A Perfect Brainstorm : { label: A PERFECT\\nBRAINSTORM\\n, height: 0.50, width: 1.11, fontsize: 10.00, fontcolor: white, pos: [ 326, 167 ], class: node, onmousedown: startdrag('1701'), length: 1, edges: { Brainwriting : { color: black, penwidth: 0.5, fontname: Arial, URL: javascript:void(predwin(2179)), label: bull;, } Ron blog.ideatree.us On Jul 14, 4:47 pm, Matt Foster mattfoste...@gmail.com wrote: Oh I get it now, attr.edges isn't an array of edge objects, its just an object with a series of properties that define an edge to your shape? If that is the case and you wish to iterate over the properties and execute set on them explicitly, you could do something like... var arr = $H(attr.edges).keys(); arr.each(function(edgeProperty){ this[edgeProperty].set(...); }, attr.edges); On Jul 14, 3:09 pm, ronman ron.new...@gmail.com wrote: Here's what I'm doing now: $A(attr.edges).each(function(edge) { edge.set(id, new GraphEdge(cv, id, edge)); }, this); Same result. The iterator.length shows zero and the inside of the loop never executes. But the iterator is (correctly, I think) 'Object Brainwriting =Object' Just casting around, I also tried taking off the binding to 'this' at the end, doesn't help. As before, I'm not getting why anything whatsoever that's in an array shouldn't automatically return length 0. On Jul 14, 1:47 pm, Matt Foster mattfoste...@gmail.com wrote: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); edge is your variable, why bother trying to set function ownership? The function is sent the parameter it needs to act upon. Just remove this from your this.edge... line and you should be good. -- http://positionabsolute.net On Jul 14, 2:28 pm, ronman ron.new...@gmail.com wrote: This is a newbie question, but I learn a lot every time I ask. Why do some objects return a zero length? I'm doing Array.each. In the array at this particular time is a single object. Firebug describes that object as Object Brainwriting color=black penwidth=0.5 fontname=Arial and I iterate through the array this way: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); But in Prototype's each method the 'edge' iterator (the Brainwriting Object), returns zero length: function $A(iterable) { if (!iterable) return []; if (iterable.toArray) return iterable.toArray(); var length = iterable.length || 0, results = new Array(length); // LENGTH NOW IS ZERO while (length--) results[length] = iterable[length];
[Proto-Scripty] Re: Objects that return zero length
Oh I get it now, attr.edges isn't an array of edge objects, its just an object with a series of properties that define an edge to your shape? If that is the case and you wish to iterate over the properties and execute set on them explicitly, you could do something like... var arr = $H(attr.edges).keys(); arr.each(function(edgeProperty){ this[edgeProperty].set(...); }, attr.edges); On Jul 14, 3:09 pm, ronman ron.new...@gmail.com wrote: Here's what I'm doing now: $A(attr.edges).each(function(edge) { edge.set(id, new GraphEdge(cv, id, edge)); }, this); Same result. The iterator.length shows zero and the inside of the loop never executes. But the iterator is (correctly, I think) 'Object Brainwriting =Object' Just casting around, I also tried taking off the binding to 'this' at the end, doesn't help. As before, I'm not getting why anything whatsoever that's in an array shouldn't automatically return length 0. On Jul 14, 1:47 pm, Matt Foster mattfoste...@gmail.com wrote: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); edge is your variable, why bother trying to set function ownership? The function is sent the parameter it needs to act upon. Just remove this from your this.edge... line and you should be good. -- http://positionabsolute.net On Jul 14, 2:28 pm, ronman ron.new...@gmail.com wrote: This is a newbie question, but I learn a lot every time I ask. Why do some objects return a zero length? I'm doing Array.each. In the array at this particular time is a single object. Firebug describes that object as Object Brainwriting color=black penwidth=0.5 fontname=Arial and I iterate through the array this way: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); But in Prototype's each method the 'edge' iterator (the Brainwriting Object), returns zero length: function $A(iterable) { if (!iterable) return []; if (iterable.toArray) return iterable.toArray(); var length = iterable.length || 0, results = new Array(length); // LENGTH NOW IS ZERO while (length--) results[length] = iterable[length]; return results; } Maybe the Brainwriting Object is not iterable, but I would expect $A (attr.edges) to have made it so before my call to each(). Surely I'm not expected to assign a length attribute to objects myself - just lilke 'penwidth' and 'color' were assigned - in order to make Array.each() work on objects. Why doesn't the Object.length return non-zero when any kind of Object is instantiated? I'm using Firefox 3.0.10. --~--~-~--~~~---~--~~ 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: Objects that return zero length
Yes, that's pretty close. attr is an object with properties, and one of those, attr.edges, is a nested object with its own nested sub- objects. All these properties and objects are constructed by evalJson () and then put in a $H() in a manner very similar to what you just described. I've copied a sample of the structure below. I was assuming that putting this whole thing within $A() would make the individual members (properties and objects) iterable, but perhaps I'm misunderstanding and instead every property and sub-object must be iterable (that is, understand the [] operator and have a length() method). Unfortunately, when the sub-object edges is empty, then running each () on the hash that contains it fails. arr.each(function(edgeProperty){ // fails if arr is an empty Hash. works if arr is an empty Array. fails if arr is a filled Array. Notice that the first edges is empty and the second one has one member: Brainwriting. from json: { nodes: { Brain Writing : { label: BRAIN\\nWRITING , height: 0.50, width: 0.83, fontsize: 10.00, fontcolor: white, pos: [ 570, 213 ], id: 1695, onmousedown: startdrag('1695'), length: 1, edges: [ ] }, A Perfect Brainstorm : { label: A PERFECT\\nBRAINSTORM\\n, height: 0.50, width: 1.11, fontsize: 10.00, fontcolor: white, pos: [ 326, 167 ], class: node, onmousedown: startdrag('1701'), length: 1, edges: { Brainwriting : { color: black, penwidth: 0.5, fontname: Arial, URL: javascript:void(predwin(2179)), label: bull;, } Ron blog.ideatree.us On Jul 14, 4:47 pm, Matt Foster mattfoste...@gmail.com wrote: Oh I get it now, attr.edges isn't an array of edge objects, its just an object with a series of properties that define an edge to your shape? If that is the case and you wish to iterate over the properties and execute set on them explicitly, you could do something like... var arr = $H(attr.edges).keys(); arr.each(function(edgeProperty){ this[edgeProperty].set(...); }, attr.edges); On Jul 14, 3:09 pm, ronman ron.new...@gmail.com wrote: Here's what I'm doing now: $A(attr.edges).each(function(edge) { edge.set(id, new GraphEdge(cv, id, edge)); }, this); Same result. The iterator.length shows zero and the inside of the loop never executes. But the iterator is (correctly, I think) 'Object Brainwriting =Object' Just casting around, I also tried taking off the binding to 'this' at the end, doesn't help. As before, I'm not getting why anything whatsoever that's in an array shouldn't automatically return length 0. On Jul 14, 1:47 pm, Matt Foster mattfoste...@gmail.com wrote: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); edge is your variable, why bother trying to set function ownership? The function is sent the parameter it needs to act upon. Just remove this from your this.edge... line and you should be good. -- http://positionabsolute.net On Jul 14, 2:28 pm, ronman ron.new...@gmail.com wrote: This is a newbie question, but I learn a lot every time I ask. Why do some objects return a zero length? I'm doing Array.each. In the array at this particular time is a single object. Firebug describes that object as Object Brainwriting color=black penwidth=0.5 fontname=Arial and I iterate through the array this way: $A(attr.edges).each(function(edge) { this.edge.set(id, new GraphEdge(cv, id, edge)); }, this); But in Prototype's each method the 'edge' iterator (the Brainwriting Object), returns zero length: function $A(iterable) { if (!iterable) return []; if (iterable.toArray) return iterable.toArray(); var length = iterable.length || 0, results = new Array(length); // LENGTH NOW IS ZERO while (length--) results[length] = iterable[length]; return results; } Maybe the Brainwriting Object is not iterable, but I would expect $A (attr.edges) to have made it so before my call to each(). Surely I'm not expected to assign a length attribute to objects myself - just lilke 'penwidth' and 'color' were assigned - in order to make Array.each() work on objects. Why doesn't the Object.length return non-zero when any kind of Object is instantiated? I'm using Firefox 3.0.10. --~--~-~--~~~---~--~~ 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