[Proto-Scripty] Re: A better way to check for an element.
Mislav's when() seems a bit redundant when called inside of document.observe('dom:loaded')... I think this topic would be interesting to discuss in the the proto-dev mailing list, ie. exceptions thrown for non-existant elements... i'd also like to see some kind of support for catching stupid coding: documents with elements that have the same id (like... div id=myDiv/div div id=myDiv/div...ugh.) I wrote a snippet that used mutation events to check the document for junk like this, but sadly, DOMContentLoaded, DOMNodeInsterted et al aren't supported widely enough to warrant production level usage. Rick On Sat, Sep 19, 2009 at 8:39 AM, Radoslav Stankov rstan...@gmail.comwrote: You can use something like my CD3.Behavior lib ( http://github.com/RStankov/controldepo-3-widgets/blob/master/src/behaviors.js ) code CD3.Behaviors({'#element:click': yourClickHandler }); // -- or -- CD3.Behaviors('#element', function(){ this.observe('click', yourClickHandler); }); // -- or ... CD3.Behaviors have really large set of possible uses /code Or ... use Mislav's when() function http://mislav.uniqpath.com/js/when-available-in-prototype/ --~--~-~--~~~---~--~~ 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: A better way to check for an element.
Thats not to say that when() isn't pretty cool :) On Sun, Sep 20, 2009 at 9:00 AM, Rick Waldron waldron.r...@gmail.comwrote: Mislav's when() seems a bit redundant when called inside of document.observe('dom:loaded')... I think this topic would be interesting to discuss in the the proto-dev mailing list, ie. exceptions thrown for non-existant elements... i'd also like to see some kind of support for catching stupid coding: documents with elements that have the same id (like... div id=myDiv/div div id=myDiv/div...ugh.) I wrote a snippet that used mutation events to check the document for junk like this, but sadly, DOMContentLoaded, DOMNodeInsterted et al aren't supported widely enough to warrant production level usage. Rick On Sat, Sep 19, 2009 at 8:39 AM, Radoslav Stankov rstan...@gmail.comwrote: You can use something like my CD3.Behavior lib ( http://github.com/RStankov/controldepo-3-widgets/blob/master/src/behaviors.js ) code CD3.Behaviors({'#element:click': yourClickHandler }); // -- or -- CD3.Behaviors('#element', function(){ this.observe('click', yourClickHandler); }); // -- or ... CD3.Behaviors have really large set of possible uses /code Or ... use Mislav's when() function http://mislav.uniqpath.com/js/when-available-in-prototype/ --~--~-~--~~~---~--~~ 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: A better way to check for an element.
You can use something like my CD3.Behavior lib ( http://github.com/RStankov/controldepo-3-widgets/blob/master/src/behaviors.js ) code CD3.Behaviors({'#element:click': yourClickHandler }); // -- or -- CD3.Behaviors('#element', function(){ this.observe('click', yourClickHandler); }); // -- or ... CD3.Behaviors have really large set of possible uses /code Or ... use Mislav's when() function http://mislav.uniqpath.com/js/when-available-in-prototype/ --~--~-~--~~~---~--~~ 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: A better way to check for an element.
Its not much different, but this is what i do: if ( $('some_button') ) { $('some_button').observe('click', function (e) { //do stuff. }); } Ideally, i wish prototype was a bit more strict about this stuff... If you're using Firebug, you could replace your $() definition: function $(element) { if (arguments.length 1) { for (var i = 0, elements = [], length = arguments.length; i length; i++) elements.push($(arguments[i])); return elements; } * if (Object.isString(element)) { var _element = element; element = document.getElementById(element); if ( element == null ) { console.log('' + _element + ' is null or does not exist'); return; } } * return Element.extend(element); } On Fri, Sep 18, 2009 at 4:17 PM, Luisgo lgo...@gmail.com wrote: Hey all, I was wondering if you can suggest a better way of achieving what this snippet does. I do it often enough that it starts bloating my code and frankly just gets annoying to type. var some_button = $('some-button') if ( some_button ) { some_button.observe(click,function(event){ event.stop(); form_object.save(); }); } I can compress it a bit by doing this: if ( some_button = $('some-button') ) { some_button.observe(click,function(event){ event.stop(); form_object.save(); }); } But that's little consolation. In an ideal world I would want to do something close to: $('some-button').observe(click,function(){ form_object.save(); }, false ); Note: that false parameter would replace event.stop() which happens often enough. Defaults to true. Or even like (I know it looks like JQuery): $('some-button').click(function(){ form_object.save(); },false); Any ideas/suggestions? Thanks! --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---