I''m having some problems with a class I am creating. My end goal is to be able to instantiate a class and use its return value as a particular object. So far, here''s my code: SomeDiv = Class.create ({ initialize: function () { obj = new Element("div"); return obj; } }); Event.observe(window, ''load'', function() { $(''otherDiv'').appendChild(new SomeDiv()); }); Firebug says: "Node cannot be inserted at the specified point in the hierarchy" so my guess is that it is not creating the div object. Any suggestions? Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Travis wrote:> I''m having some problems with a class I am creating. My end goal is > to be able to instantiate a class and use its return value as a > particular object. > > So far, here''s my code: > > SomeDiv = Class.create ({...}); > > Event.observe(window, ''load'', function() { > $(''otherDiv'').appendChild(new SomeDiv()); > }); > > Firebug says: "Node cannot be inserted at the specified point in the > hierarchy" so my guess is that it is not creating the div object. Any > suggestions? Thanks!Travis, Not sure exactly what functionality you''re trying to achieve, but using the "new" keyword will always return an object of that instance. Element#appendChild requires that the argument given be a DOM Element. You''ll need to do something along these lines: SomeDiv = Class.create({ initialize: function(msgNode) { this.node = new Element("div"); this.msg = $(msgNode); // add some custom method to our node this.node.manipulate = this.manipulate.bind(this); // garbage collect Event.observe(window, ''unload'', function() { this.node.manipulate = null; }.bind(this)); }, manipulate: function(style, html) { // do stuff to the node this.node.setStyle(style); this.node.update(html); this.msg.update(''manipulated''); } }); Event.observe(window, ''load'', function() { $(''otherDiv'').appendChild(new SomeDiv().node); }); - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I guess a similar technique that does work would be: $(''otherDiv'').appendChild(new Element("div")); Basically I just want to a create a class that creates an Element object and then do some other things with the Element object before it returns it. On Apr 25, 3:54 pm, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Travis wrote: > > I''m having some problems with a class I am creating. My end goal is > > to be able to instantiate a class and use its return value as a > > particular object. > > > So far, here''s my code: > > > SomeDiv = Class.create ({...}); > > > Event.observe(window, ''load'', function() { > > $(''otherDiv'').appendChild(new SomeDiv()); > > }); > > > Firebug says: "Node cannot be inserted at the specified point in the > > hierarchy" so my guess is that it is not creating the div object. Any > > suggestions? Thanks! > > Travis, > > Not sure exactly what functionality you''re trying to achieve, but using > the "new" keyword will always return an object of that instance. > Element#appendChild requires that the argument given be a DOM Element. > You''ll need to do something along these lines: > > SomeDiv = Class.create({ > initialize: function(msgNode) { > this.node = new Element("div"); > this.msg = $(msgNode); > // add some custom method to our node > this.node.manipulate = this.manipulate.bind(this); > // garbage collect > Event.observe(window, ''unload'', function() { > this.node.manipulate = null; > }.bind(this)); > }, > manipulate: function(style, html) { > // do stuff to the node > this.node.setStyle(style); > this.node.update(html); > this.msg.update(''manipulated''); > } > > }); > > Event.observe(window, ''load'', function() { > $(''otherDiv'').appendChild(new SomeDiv().node); > > }); > > - Ken Snyder- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you for the response Ken. It doesn''t seem to update the div with ''manipulated''. Maybe its not suppose to? If not, how would I do that. Thanks! On Apr 25, 3:54 pm, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Travis wrote: > > I''m having some problems with a class I am creating. My end goal is > > to be able to instantiate a class and use its return value as a > > particular object. > > > So far, here''s my code: > > > SomeDiv = Class.create ({...}); > > > Event.observe(window, ''load'', function() { > > $(''otherDiv'').appendChild(new SomeDiv()); > > }); > > > Firebug says: "Node cannot be inserted at the specified point in the > > hierarchy" so my guess is that it is not creating the div object. Any > > suggestions? Thanks! > > Travis, > > Not sure exactly what functionality you''re trying to achieve, but using > the "new" keyword will always return an object of that instance. > Element#appendChild requires that the argument given be a DOM Element. > You''ll need to do something along these lines: > > SomeDiv = Class.create({ > initialize: function(msgNode) { > this.node = new Element("div"); > this.msg = $(msgNode); > // add some custom method to our node > this.node.manipulate = this.manipulate.bind(this); > // garbage collect > Event.observe(window, ''unload'', function() { > this.node.manipulate = null; > }.bind(this)); > }, > manipulate: function(style, html) { > // do stuff to the node > this.node.setStyle(style); > this.node.update(html); > this.msg.update(''manipulated''); > } > > }); > > Event.observe(window, ''load'', function() { > $(''otherDiv'').appendChild(new SomeDiv().node); > > }); > > - Ken Snyder- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Travis, Returning values from initialize() is not supported. If you want this behavior here is a patch to achieve it: http://dev.rubyonrails.org/ticket/11481 you could also do: var foo = new MyClass.toSomeValue(); - JDD --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---