Rahul Bhargava
2005-Aug-29 12:05 UTC
[Rails-spinoffs] can a class find out it''s instance name?
Hi friends - a javascript question....
Does the prototype object add some way for a class to find out the
variable name of its instance variable?
Or does javascript have some built in way to do this?
This is confusing to describe - but if I define a variable as some
object:
var myVariable= new Widget(''fdfa'');
can some built-in method inside the Widget class be called to find out
that this particular instance is named "myVariable"?
The reason is that this widget writes some html elements with the
Builder. I want one of the elements it writes to be a link to call a
method on itself when clicked. I don''t want to hardcode the
"myObject.doSomething()" call into there. Binding stuff
doesn''t help me
because I still need to reference some variable to make the call on in
the link.
Hope that isn''t too poorly described!
Thanks for any insight or suggestions,
rahul
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails-spinoffs/attachments/20050829/ab9c1042/attachment.html
Marc Juul Christoffersen
2005-Aug-30 14:59 UTC
[Rails-spinoffs] can a class find out it''s instance name?
maybe i''m not understanding the problem, but i don''t see why you can''t use .bind(). from inside the object, why not do: myHtmlElement.onclick = this.doSomething.bind(this); ? Juul
Michael Schuerig
2005-Aug-30 15:43 UTC
[Rails-spinoffs] Re: can a class find out it''s instance name?
On Monday 29 August 2005 19:08, Rahul Bhargava wrote:> This is confusing to describe - but if I define a variable as some > object: > > var myVariable= new Widget(''fdfa''); > > can some built-in method inside the Widget class be called to find > out that this particular instance is named "myVariable"?Consider var myVariable= new Widget(''fdfa''); var myAlias = myVariable; What reference to the same object would you like to get?> The reason is that this widget writes some html elements with the > Builder. I want one of the elements it writes to be a link to call a > method on itself when clicked. I don''t want to hardcode the > "myObject.doSomething()" call into there. Binding stuff doesn''t help > me because I still need to reference some variable to make the call > on in the link.I don''t fully understand what you''re trying to achieve, but if you want to have your newly created element to call back into your object, then binding is exactly the right thing to use: var Widget = Class.create(); Widget.prototype = { initialize: function(myname) { this.name = myname; var link = Builder.node(''a''); Event.observe(link, ''click'', this.onClick.bindAsEventListener(this)); }, onClick: function(event) { alert(''I''ve been clicked: '' + this.name); Event.stop(event); } } Michael -- Michael Schuerig Not only does lightning not strike mailto:michael@schuerig.de twice, it usually doesn''t strike once. http://www.schuerig.de/michael/ --Salman Rushdie, Fury
Jon Tirsen
2005-Aug-30 23:29 UTC
[Rails-spinoffs] can a class find out it''s instance name?
Short answer: No, not a chance. :-) On 8/30/05, Rahul Bhargava <rahul@ntag.com> wrote:> > Hi friends ? a javascript question?. > > Does the prototype object add some way for a class to find out the > variable name of its instance variable? > > Or does javascript have some built in way to do this? > > This is confusing to describe - but if I define a variable as some > object: > > var myVariable= new Widget(''fdfa''); > > can some built-in method inside the Widget class be called to find out > that this particular instance is named "myVariable"? > > The reason is that this widget writes some html elements with the > Builder. I want one of the elements it writes to be a link to call a method > on itself when clicked. I don''t want to hardcode the > "myObject.doSomething()" call into there. Binding stuff doesn''t help me > because I still need to reference some variable to make the call on in the > link. > > Hope that isn''t too poorly described! > > Thanks for any insight or suggestions, > > rahul > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails-spinoffs/attachments/20050830/e5fc1d99/attachment.html
Bruce Collier
2005-Aug-31 19:19 UTC
[Rails-spinoffs] can a class find out it''s instance name?
I think you need to rethink what you''re doing... even if you do get
this to
work it will be confusing for the next guy who has to maintain your code
(and you should always assume that someone else will)
Why don''t you want to ''hard code'' the method call in
there?
-----Original Message-----
From: rails-spinoffs-bounces@lists.rubyonrails.org
[mailto:rails-spinoffs-bounces@lists.rubyonrails.org]On Behalf Of Jon Tirsen
Sent: Wednesday, 31 August 2005 2:26 PM
To: rails-spinoffs@lists.rubyonrails.org
Subject: Re: [Rails-spinoffs] can a class find out it''s instance name?
Short answer: No, not a chance. :-)
On 8/30/05, Rahul Bhargava < rahul@ntag.com <mailto:rahul@ntag.com>
> wrote:
Hi friends ? a javascript question
.
Does the prototype object add some way for a class to find out the variable
name of its instance variable?
Or does javascript have some built in way to do this?
This is confusing to describe - but if I define a variable as some object:
var myVariable= new Widget(''fdfa'');
can some built-in method inside the Widget class be called to find out that
this particular instance is named "myVariable"?
The reason is that this widget writes some html elements with the Builder.
I want one of the elements it writes to be a link to call a method on itself
when clicked. I don''t want to hardcode the
"myObject.doSomething()" call
into there. Binding stuff doesn''t help me because I still need to
reference
some variable to make the call on in the link.
Hope that isn''t too poorly described!
Thanks for any insight or suggestions,
rahul
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
<mailto:Rails-spinoffs@lists.rubyonrails.org>
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
<http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails-spinoffs/attachments/20050831/89730e83/attachment.html
Rahul Bhargava
2005-Sep-01 08:19 UTC
[Rails-spinoffs] Re: can a class find out it''s instance name?
I guess I didn''t explain it well enough. I also think I''ve
got an
overcomplicated solution, but my question is still a valid one (though I
think I it''s been answered already ;-).
I have an object that writes some HTML to a div element when a link is
clicked. I want that html that is written to be able to call a method
in that instance of the object. It seems there is no way for an object,
when it is running a method, to know the name of the instance that is
running that method.
Here''s a psuedocode example:
<script>
MyObj = new Class()
MyObj.prototype={
writeSomeStuff:function(){
var myInstanceName = "myVar";
var html = "<a
href=\""+myInstanceName+".foo();\">";
html+= "foo it</a>";
$(''dest'').innerHTML = html;
}
foo:function(){
//do some other stuff
}
}
var myVar = new MyObj();
</script>
<a href="#"
onClick="myVar.writeSomeStuff(''dest'');">writeStuff</a>
<div id="dest">
</div>
I''m pretty sure that there is no way to automatically replace the
myInstanceName var with some automatic call. The solution I''d use
would
be to call a method on MyObj that stores the name of the instance just
after creating the instance. Or use bind to do something similar -
right?
Thanks,
rahul
-----Original Message-----
From: rails-spinoffs-bounces@lists.rubyonrails.org
[mailto:rails-spinoffs-bounces@lists.rubyonrails.org] On Behalf Of
Michael Schuerig
Sent: Tuesday, August 30, 2005 4:41 PM
To: rails-spinoffs@lists.rubyonrails.org
Subject: [Rails-spinoffs] Re: can a class find out it''s instance name?
On Monday 29 August 2005 19:08, Rahul Bhargava wrote:
> This is confusing to describe - but if I define a variable as some
> object:
>
> var myVariable= new Widget(''fdfa'');
>
> can some built-in method inside the Widget class be called to find
> out that this particular instance is named "myVariable"?
Consider
var myVariable= new Widget(''fdfa'');
var myAlias = myVariable;
What reference to the same object would you like to get?
> The reason is that this widget writes some html elements with the
> Builder. I want one of the elements it writes to be a link to call a
> method on itself when clicked. I don''t want to hardcode the
> "myObject.doSomething()" call into there. Binding stuff
doesn''t help
> me because I still need to reference some variable to make the call
> on in the link.
I don''t fully understand what you''re trying to achieve, but if
you want
to have your newly created element to call back into your object, then
binding is exactly the right thing to use:
var Widget = Class.create();
Widget.prototype = {
initialize: function(myname) {
this.name = myname;
var link = Builder.node(''a'');
Event.observe(link, ''click'',
this.onClick.bindAsEventListener(this));
},
onClick: function(event) {
alert(''I''ve been clicked: '' + this.name);
Event.stop(event);
}
}
Michael
--
Michael Schuerig Not only does lightning not strike
mailto:michael@schuerig.de twice, it usually doesn''t strike once.
http://www.schuerig.de/michael/ --Salman Rushdie, Fury
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Laurie Harper
2005-Sep-01 21:31 UTC
[Rails-spinoffs] Re: can a class find out it''s instance name?
As somebody else already asked, why can''t you use ''this''? As in var html = "<a href=\""+ this +".foo();\">"; L. On 1-Sep-05, at 9:07 AM, Rahul Bhargava wrote:> I guess I didn''t explain it well enough. I also think I''ve got an > overcomplicated solution, but my question is still a valid one > (though I > think I it''s been answered already ;-). > > I have an object that writes some HTML to a div element when a link is > clicked. I want that html that is written to be able to call a method > in that instance of the object. It seems there is no way for an > object, > when it is running a method, to know the name of the instance that is > running that method. > > Here''s a psuedocode example: > > <script> > MyObj = new Class() > MyObj.prototype={ > writeSomeStuff:function(){ > var myInstanceName = "myVar"; > var html = "<a href=\""+myInstanceName+".foo();\">"; > html+= "foo it</a>"; > $(''dest'').innerHTML = html; > } > foo:function(){ > //do some other stuff > } > } > var myVar = new MyObj(); > </script> > > <a href="#" onClick="myVar.writeSomeStuff(''dest'');">writeStuff</a> > > <div id="dest"> > </div> > > I''m pretty sure that there is no way to automatically replace the > myInstanceName var with some automatic call. The solution I''d use > would > be to call a method on MyObj that stores the name of the instance just > after creating the instance. Or use bind to do something similar - > right? > > Thanks, > > rahul > > -----Original Message----- > From: rails-spinoffs-bounces@lists.rubyonrails.org > [mailto:rails-spinoffs-bounces@lists.rubyonrails.org] On Behalf Of > Michael Schuerig > Sent: Tuesday, August 30, 2005 4:41 PM > To: rails-spinoffs@lists.rubyonrails.org > Subject: [Rails-spinoffs] Re: can a class find out it''s instance name? > > On Monday 29 August 2005 19:08, Rahul Bhargava wrote: > > >> This is confusing to describe - but if I define a variable as some >> object: >> >> var myVariable= new Widget(''fdfa''); >> >> can some built-in method inside the Widget class be called to find >> out that this particular instance is named "myVariable"? >> > > Consider > > var myVariable= new Widget(''fdfa''); > var myAlias = myVariable; > > What reference to the same object would you like to get? > > >> The reason is that this widget writes some html elements with the >> Builder. I want one of the elements it writes to be a link to call a >> method on itself when clicked. I don''t want to hardcode the >> "myObject.doSomething()" call into there. Binding stuff doesn''t help >> me because I still need to reference some variable to make the call >> on in the link. >> > > I don''t fully understand what you''re trying to achieve, but if you > want > to have your newly created element to call back into your object, then > binding is exactly the right thing to use: > > var Widget = Class.create(); > Widget.prototype = { > initialize: function(myname) { > this.name = myname; > var link = Builder.node(''a''); > Event.observe(link, ''click'', > this.onClick.bindAsEventListener(this)); > }, > onClick: function(event) { > alert(''I''ve been clicked: '' + this.name); > Event.stop(event); > } > } > > Michael > > -- > Michael Schuerig Not only does lightning not strike > mailto:michael@schuerig.de twice, it usually doesn''t strike > once. > http://www.schuerig.de/michael/ --Salman Rushdie, Fury > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >-- Laurie Harper Open Source advocate, Java geek: http://www.holoweb.net/laurie Founder, Zotech Software: http://www.zotechsoftware.com/
Marc Juul Christoffersen
2005-Sep-01 21:53 UTC
[Rails-spinoffs] Re: can a class find out it''s instance name?
just use bind.
it''s true that you can''t get the instance name, and so, what
you''re
proposing is "impossible".
but if you instead of creating the html as a string, create the
individual elements with document.createElement(''tagname'');
then you
can simply assign the .onclick event of the created element using
bind. ie:
// inside an object
var node = document.createElement(''DIV'');
node.onclick = this.someFunc.bind(this);
$(''myContainer'').appendChild(node);
just try to avoid creating html from a string unless you really really
have to, or if you do, give everything an unique id so you can get a
reference to it from javascript after creating.
Juul
On 9/1/05, Rahul Bhargava <rahul@ntag.com> wrote:> I guess I didn''t explain it well enough. I also think
I''ve got an
> overcomplicated solution, but my question is still a valid one (though I
> think I it''s been answered already ;-).
>
> I have an object that writes some HTML to a div element when a link is
> clicked. I want that html that is written to be able to call a method
> in that instance of the object. It seems there is no way for an object,
> when it is running a method, to know the name of the instance that is
> running that method.
>
> Here''s a psuedocode example:
>
> <script>
> MyObj = new Class()
> MyObj.prototype={
> writeSomeStuff:function(){
> var myInstanceName = "myVar";
> var html = "<a
href=\""+myInstanceName+".foo();\">";
> html+= "foo it</a>";
> $(''dest'').innerHTML = html;
> }
> foo:function(){
> //do some other stuff
> }
> }
> var myVar = new MyObj();
> </script>
>
> <a href="#"
onClick="myVar.writeSomeStuff(''dest'');">writeStuff</a>
>
> <div id="dest">
> </div>
>
> I''m pretty sure that there is no way to automatically replace the
> myInstanceName var with some automatic call. The solution I''d use
would
> be to call a method on MyObj that stores the name of the instance just
> after creating the instance. Or use bind to do something similar -
> right?
>
> Thanks,
>
> rahul
>
> -----Original Message-----
> From: rails-spinoffs-bounces@lists.rubyonrails.org
> [mailto:rails-spinoffs-bounces@lists.rubyonrails.org] On Behalf Of
> Michael Schuerig
> Sent: Tuesday, August 30, 2005 4:41 PM
> To: rails-spinoffs@lists.rubyonrails.org
> Subject: [Rails-spinoffs] Re: can a class find out it''s instance
name?
>
> On Monday 29 August 2005 19:08, Rahul Bhargava wrote:
>
> > This is confusing to describe - but if I define a variable as some
> > object:
> >
> > var myVariable= new Widget(''fdfa'');
> >
> > can some built-in method inside the Widget class be called to find
> > out that this particular instance is named "myVariable"?
>
> Consider
>
> var myVariable= new Widget(''fdfa'');
> var myAlias = myVariable;
>
> What reference to the same object would you like to get?
>
> > The reason is that this widget writes some html elements with the
> > Builder. I want one of the elements it writes to be a link to call a
> > method on itself when clicked. I don''t want to hardcode the
> > "myObject.doSomething()" call into there. Binding stuff
doesn''t help
> > me because I still need to reference some variable to make the call
> > on in the link.
>
> I don''t fully understand what you''re trying to achieve,
but if you want
> to have your newly created element to call back into your object, then
> binding is exactly the right thing to use:
>
> var Widget = Class.create();
> Widget.prototype = {
> initialize: function(myname) {
> this.name = myname;
> var link = Builder.node(''a'');
> Event.observe(link, ''click'',
> this.onClick.bindAsEventListener(this));
> },
> onClick: function(event) {
> alert(''I''ve been clicked: '' + this.name);
> Event.stop(event);
> }
> }
>
> Michael
>
> --
> Michael Schuerig Not only does lightning not strike
> mailto:michael@schuerig.de twice, it usually doesn''t strike
once.
> http://www.schuerig.de/michael/ --Salman Rushdie, Fury
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
Bruce Collier
2005-Sep-01 23:45 UTC
[Rails-spinoffs] Re: can a class find out it''s instance name?
You could just do this:
writeSomeStuff:function(){
var a = document.createElement("a");
a.parentObject = this;
a.onclick = function() {
this.parentObject.foo();
return false;
}
a.innerHTML = "foo it";
$(''dest'').appendChild(a);
}
-----Original Message-----
From: rails-spinoffs-bounces@lists.rubyonrails.org
[mailto:rails-spinoffs-bounces@lists.rubyonrails.org]On Behalf Of Rahul
Bhargava
Sent: Thursday, 1 September 2005 11:07 PM
To: rails-spinoffs@lists.rubyonrails.org
Subject: RE: [Rails-spinoffs] Re: can a class find out it''s instance
name?
<p align=\"left\"><b><font face=\"Arial\"
size=\"2\">GFI MailSecurity''s HTML
threat engine found HTML scripts in this email and has disabled
them.</font></b></p>I guess I didn''t explain it well
enough. I also think
I''ve got an
overcomplicated solution, but my question is still a valid one (though I
think I it''s been answered already ;-).
I have an object that writes some HTML to a div element when a link is
clicked. I want that html that is written to be able to call a method
in that instance of the object. It seems there is no way for an object,
when it is running a method, to know the name of the instance that is
running that method.
Here''s a psuedocode example:
<Xcript><!--
MyObj = new Class()
MyObj.prototype={
writeSomeStuff:function(){
var myInstanceName = "myVar";
var html = "<a
href=\""+myInstanceName+".foo();\">";
html+= "foo it</a>";
$(''dest'').innerHTML = html;
}
foo:function(){
//do some other stuff
}
}
var myVar = new MyObj();
--></Xcript>
<a href="#"
onClick="myVar.writeSomeStuff(''dest'');">writeStuff</a>
<div id="dest">
</div>
I''m pretty sure that there is no way to automatically replace the
myInstanceName var with some automatic call. The solution I''d use
would
be to call a method on MyObj that stores the name of the instance just
after creating the instance. Or use bind to do something similar -
right?
Thanks,
rahul
-----Original Message-----
From: rails-spinoffs-bounces@lists.rubyonrails.org
[mailto:rails-spinoffs-bounces@lists.rubyonrails.org] On Behalf Of
Michael Schuerig
Sent: Tuesday, August 30, 2005 4:41 PM
To: rails-spinoffs@lists.rubyonrails.org
Subject: [Rails-spinoffs] Re: can a class find out it''s instance name?
On Monday 29 August 2005 19:08, Rahul Bhargava wrote:
> This is confusing to describe - but if I define a variable as some
> object:
>
> var myVariable= new Widget(''fdfa'');
>
> can some built-in method inside the Widget class be called to find
> out that this particular instance is named "myVariable"?
Consider
var myVariable= new Widget(''fdfa'');
var myAlias = myVariable;
What reference to the same object would you like to get?
> The reason is that this widget writes some html elements with the
> Builder. I want one of the elements it writes to be a link to call a
> method on itself when clicked. I don''t want to hardcode the
> "myObject.doSomething()" call into there. Binding stuff
doesn''t help
> me because I still need to reference some variable to make the call
> on in the link.
I don''t fully understand what you''re trying to achieve, but if
you want
to have your newly created element to call back into your object, then
binding is exactly the right thing to use:
var Widget = Class.create();
Widget.prototype = {
initialize: function(myname) {
this.name = myname;
var link = Builder.node(''a'');
Event.observe(link, ''click'',
this.onClick.bindAsEventListener(this));
},
onClick: function(event) {
alert(''I''ve been clicked: '' + this.name);
Event.stop(event);
}
}
Michael
--
Michael Schuerig Not only does lightning not strike
mailto:michael@schuerig.de twice, it usually doesn''t strike once.
http://www.schuerig.de/michael/ --Salman Rushdie, Fury
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails-spinoffs/attachments/20050901/ccea4185/attachment.html
Sam Foster
2005-Sep-07 22:59 UTC
[Rails-spinoffs] Re: can a class find out it''s instance name?
Marc Juul Christoffersen wrote:>just use bind. >it''s true that you can''t get the instance name, and so, what you''re >proposing is "impossible". > >one workaround is to keep all your instances somewhere global e.g. window.widgets = {}; then when you instantiate: window.widgets["instanceName"] = new Widget(); so you have another way to get at a particular widget. You can get back inside the instance scope with bind: function() { // more stuff... }.bind(window.widgets["instanceName"]) hth? Sam