[Prototype-core] Re: Enumerable.pluck()-like setter
On Aug 17, 2007, at 12:22 AM, Mislav Marohnić wrote: It's nice to hear about how all of you solved the problem of collection attribute assignment, but the variety of solutions shows that this is very app-specific (IMO). I won't argue against not including (invoke-ing writeAttribute works just fine), but I find your reasoning extremely odd. I would think the variety of solutions actually demonstrates a wide-spread need, not an app-specific one. (If you were to call it a training/ experience issue, pointing to invoke's ability to accept multiple parameters, then I'd agree wholeheartedly, and not complain. =) I've never needed this myself; and if I have, it would only be in one or two places, which hardly justifies its inclusion in the core framework. ... and I've never needed $R, PeriodicalExecuter or Ajax.PeriodicalUpdater. At all. Not even once. Doesn't mean they're not useful. I'm being a bit argumentative ... Like I said, I have no grief over the conclusion, but I think your reasons are off. =) TAG --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
On 8/17/07, kangax [EMAIL PROTECTED] wrote: Writing to attributes is fine but for custom properties I ended up with this little helper (I know the name is idiotic but I had no time to think of a good one): Element.addMethods({ __extend: function(element, hash) { return Object.extend($(element), hash) } }); It's nice to hear about how all of you solved the problem of collection attribute assignment, but the variety of solutions shows that this is very app-specific (IMO). I've never needed this myself; and if I have, it would only be in one or two places, which hardly justifies its inclusion in the core framework. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
Tom Gregory wrote: ... apply: function(iterator, attribute, value) { return this.map(function(item, index) { item[attribute] = value; }); }, ... I've run into a need for this as well. I'd vote for including it under a name 'setAll' or 'setEach'. Or, what is the exact opposite of pluck? Pin? Place? Affix? :P - Ken Snyder --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
I would definitely second the nomination of a name that actually means something, like 'setAttribute' or similar. But then I'm a fan of self documenting function names - even when they're long and include all the letters in words. On Aug 15, 2007, at 8:58 AM, Ken Snyder wrote: Tom Gregory wrote: ... apply: function(iterator, attribute, value) { return this.map(function(item, index) { item[attribute] = value; }); }, ... I've run into a need for this as well. I'd vote for including it under a name 'setAll' or 'setEach'. Or, what is the exact opposite of pluck? Pin? Place? Affix? :P - Ken Snyder --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
On Aug 15, 2007, at 9:58 AM, Ken Snyder wrote: Tom Gregory wrote: apply: function(iterator, attribute, value) { return this.map(function(item, index) { item[attribute] = value; }); }, I've run into a need for this as well. I'd vote for including it under a name 'setAll' or 'setEach'. I've pretty much talked myself out of the need for it, using invoke and writeAttribute instead, although a shortcut would be nice. In order to be useful for dealing with arrays of Elements, it should probably leverage the new writeAttribute (to take advantage of Element._attributeTranslations) if Elements are detected. If it was done that way, why not be keep things simple and call it 'writeAttribute' (similar to Jeff's suggestion). Or, what is the exact opposite of pluck? Pin? Place? Affix? :P Plant? TAG --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
I am not a member of core, but +1 I like this for simplicity, but you could possibly use invoke? $$('#myFormId input').invoke('setAttribute','disabled',true) Gareth On 8/15/07, Tom Gregory [EMAIL PROTECTED] wrote: It might be something easy that I'm overlooking, but I don't see what I'm looking for as part of either Enumerable or Array. I've found myself needing to set attributes on arrays of Elements. I know this is trivial with each(), but I'm looking for a cleaner way to do it. The idea is similar to Enumerable.pluck(), but as a setter instead of a getter. Perhaps there is already an easy way to do it that I'm missing. I'd call it Enumerable.apply() or .setEach()/ extendEach(). apply: function(iterator, attribute, value) { return this.map(function(item, index) { item[attribute] = value; }); }, Use case: $$('#myFormId input').apply('disabled', true); $$('#myFormId input').apply('checked', ''); It could be made a bit more robust with something like this: apply: function(iterator, hash) { return this.map(function(item, index) { Object.extend(item, hash); }); }, Even better, the function could be reworked to auto-detect the number of parameters passed (or whether the first is a string or an object), and act accordingly. Useful? Not useful? I'm an idiot and overlooked something simple? Your feedback is encouraged. TAG --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
Okay... so maybe an array of Elements isn't the best example case. As I've thought about this a bit more, there's an easy way to accomplish this with Elements using invoke() $$(selector).invoke('writeAttribute', 'value', 'foo'); ... but what about arrays of Hashes/Objects where there isn't a generic setter function defined? Maybe implementing the concept as I've suggested below will lead to lower quality code on the user side if the workarounds in (for example) 'writeAttribute' aren't applied... It looks like I jumped the gun a bit, but the question still remains: would such an idea be useful when dealing with arrays of hashes, or is using each() so trivial as to not bother with another helper? TAG On Aug 14, 2007, at 5:20 PM, Tom Gregory wrote: It might be something easy that I'm overlooking, but I don't see what I'm looking for as part of either Enumerable or Array. I've found myself needing to set attributes on arrays of Elements. I know this is trivial with each(), but I'm looking for a cleaner way to do it. The idea is similar to Enumerable.pluck(), but as a setter instead of a getter. Perhaps there is already an easy way to do it that I'm missing. I'd call it Enumerable.apply() or .setEach ()/extendEach(). apply: function(iterator, attribute, value) { return this.map(function(item, index) { item[attribute] = value; }); }, Use case: $$('#myFormId input').apply('disabled', true); $$('#myFormId input').apply('checked', ''); It could be made a bit more robust with something like this: apply: function(iterator, hash) { return this.map(function(item, index) { Object.extend(item, hash); }); }, Even better, the function could be reworked to auto-detect the number of parameters passed (or whether the first is a string or an object), and act accordingly. Useful? Not useful? I'm an idiot and overlooked something simple? Your feedback is encouraged. TAG --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
Nice one, Tom! As far as naming I'd rather go with same pluck but with optional second argument for setting value $$('#myFormId input').pluck('disabled'); // getter $$('#myFormId input').pluck('disabled', true); // setter You have no idea how much confusion there's among beginners as far as figuring out all these weird Enumerable methods (specially if you're not familiar with Ruby). Introducing another one, I believe, would bring even more chaos. P.S. Since we're on this subject, has anyone ever considered having case- insensitive flag for Array's without method. $('nav').descendants().pluck('tagName').without('LI') (i.e. when dealing with HTML vs XML case issues) I know, this case is pretty trivial and could be simply done with selectors: $$('#nav li a') but wouldn't it be still usefull to have such option? Best, kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Enumerable.pluck()-like setter
I was in a similar situation where a pluck like setter would have been great. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype: Core group. To post to this group, send email to prototype-core@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-core?hl=en -~--~~~~--~~--~--~---