Hi,
I''m unable to get the invoke method to work inside an instance of an
object, when the method I''m trying to invoke needs the
''this'' keyword
in order for the method to be identified.
Example:
var myClass = Class.create();
myClass.prototype = {
intialize:function() {
// ...do stuff
},
getRows:function() {
// invoke using this.setupRow doesn''t work
$$(''.rows'').invoke(''this.setupRow'').bind(this);
},
setupRow:function(row) {
// ...do stuff to row
}
}
var myClassI = new myClass();
---------------------------------------------------
In the above example, when I attempt to use invoke to call a method
inside the object, it doesn''t work. I''ve tried without bind,
and I''ve
tried moving bind around to everywhere I could think of, and not using
single quotes:
$$(''.rows'').invoke(''this.setupRow.bind(this)'');
$$(''.rows'').invoke(this.setupRow.bind(this));
$$(''.rows'').invoke(this.setupRow).bind(this);
$$(''.rows'').invoke(this.setupRow);
etc, etc
I managed a simple workaround, but it''s using Enumerable#each instead
of Enumerable#invoke. It isn''t the end of the world, but if
it''s
possible to use the more efficient invoke method, I''d much prefer it.
workaround:
$$(''.rows'').each(function(row)
{this.setupRow(row);}.bind(this);
Some things to note. Looking at the invoke method in prototype.js, it
uses ''this'', which refers to the page in my example. I think
it''s not
supposed to work like I''m trying to use it. But, I don''t want
to jump
to conclusions if anyone else knows differently.
The ''this'' keyword is properly referring to my instance of
myClass
within the getRows() method. I verified this several ways, but
probably most importantly using firebug in firefox.
The JS error that I receive is:
TypeError: value[method] has no properties line 708
prototype version: 1.6.0.1
If I use invoke to call a method like ''hide'' or any other
global scope
method, it works fine.
As I said, I have a suitable workaround, but I''m just not sure why
invoke isn''t working like I expect it to here. Am I doing it wrong?
Is it not supposed to work this way? Or is it a bug?
Thanks for any help.
Knox
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Knox,
#invoke calls method *of* an element.
#each on the other hand calls method, *passing* it an element.
#each also accepts optional "context" argument to bind iterator to.
This should work in your case:
$$(''.rows'').each(this.setupRow, this);
Best,
kangax
On Mar 6, 6:12 pm, knox
<krowel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi,
>
> I''m unable to get the invoke method to work inside an instance of
an
> object, when the method I''m trying to invoke needs the
''this'' keyword
> in order for the method to be identified.
>
> Example:
> var myClass = Class.create();
> myClass.prototype = {
> intialize:function() {
> // ...do stuff
> },
> getRows:function() {
> // invoke using this.setupRow doesn''t work
>
$$(''.rows'').invoke(''this.setupRow'').bind(this);
> },
> setupRow:function(row) {
> // ...do stuff to row
> }}
>
> var myClassI = new myClass();
>
> ---------------------------------------------------
> In the above example, when I attempt to use invoke to call a method
> inside the object, it doesn''t work. I''ve tried without
bind, and I''ve
> tried moving bind around to everywhere I could think of, and not using
> single quotes:
>
$$(''.rows'').invoke(''this.setupRow.bind(this)'');
> $$(''.rows'').invoke(this.setupRow.bind(this));
> $$(''.rows'').invoke(this.setupRow).bind(this);
> $$(''.rows'').invoke(this.setupRow);
> etc, etc
>
> I managed a simple workaround, but it''s using Enumerable#each
instead
> of Enumerable#invoke. It isn''t the end of the world, but if
it''s
> possible to use the more efficient invoke method, I''d much prefer
it.
> workaround:
> $$(''.rows'').each(function(row)
{this.setupRow(row);}.bind(this);
>
> Some things to note. Looking at the invoke method in prototype.js, it
> uses ''this'', which refers to the page in my example. I
think it''s not
> supposed to work like I''m trying to use it. But, I don''t
want to jump
> to conclusions if anyone else knows differently.
> The ''this'' keyword is properly referring to my instance
of myClass
> within the getRows() method. I verified this several ways, but
> probably most importantly using firebug in firefox.
> The JS error that I receive is:
> TypeError: value[method] has no properties line 708
> prototype version: 1.6.0.1
> If I use invoke to call a method like ''hide'' or any other
global scope
> method, it works fine.
>
> As I said, I have a suitable workaround, but I''m just not sure why
> invoke isn''t working like I expect it to here. Am I doing it
wrong?
> Is it not supposed to work this way? Or is it a bug?
>
> Thanks for any help.
> Knox
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
knox wrote:> Hi, > > I''m unable to get the invoke method to work inside an instance of an > object, when the method I''m trying to invoke needs the ''this'' keyword > in order for the method to be identified. > > Example: > var myClass = Class.create(); > myClass.prototype = { > intialize:function() { > // ...do stuff > }, > getRows:function() { > // invoke using this.setupRow doesn''t work > $$(''.rows'').invoke(''this.setupRow'').bind(this); > }, > setupRow:function(row) { > // ...do stuff to row > } > } > var myClassI = new myClass(); > > ... >You don''t need invoke, you need a simple each. $$(''.rows'').each(this.setupRow.bind(this)); And you only need to use ".bind(this)" if your setupRow function uses the "this" keyword. What you''re doing with invoke is calling each element''s method named "this.setupRow" but the elements don''t have a method with that name. In another way, you are doing this: var rows = $$(''.rows''); for (var i = 0; i < rows.length; i++) { // call rows[i]''s method called ''this.setupRow'' with no arguments rows[i][''this.setupRow''](); } When what you want to do is use each--which is the same as: var rows = $$(''.rows''); for (var i = 0; i < rows.length; i++) { // call this.setupRows with rows[i] as the first argument and i as the second argument this.setupRow(rows[i], i); } - 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 -~----------~----~----~----~------~----~------~--~---
Thanks, that makes sense now. I didn''t realize that the called method needed to be a method of the element itself. I was already using each as a workaround. But, I wasn''t sure if I was using invoke wrong, or if invoke wasn''t meant to do what I wanted. Thanks again, Knox On Mar 6, 4:49 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Knox, > > #invoke calls method *of* an element. > #each on the other hand calls method, *passing* it an element. > #each also accepts optional "context" argument to bind iterator to. > > This should work in your case: > > $$(''.rows'').each(this.setupRow, this); > > Best, > kangax > > On Mar 6, 6:12 pm, knox <krowel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > I''m unable to get the invoke method to work inside an instance of an > > object, when the method I''m trying to invoke needs the ''this'' keyword > > in order for the method to be identified. > > > Example: > > var myClass = Class.create(); > > myClass.prototype = { > > intialize:function() { > > // ...do stuff > > }, > > getRows:function() { > > // invoke using this.setupRow doesn''t work > > $$(''.rows'').invoke(''this.setupRow'').bind(this); > > }, > > setupRow:function(row) { > > // ...do stuff to row > > }} > > > var myClassI = new myClass(); > > > --------------------------------------------------- > > In the above example, when I attempt to use invoke to call a method > > inside the object, it doesn''t work. I''ve tried without bind, and I''ve > > tried moving bind around to everywhere I could think of, and not using > > single quotes: > > $$(''.rows'').invoke(''this.setupRow.bind(this)''); > > $$(''.rows'').invoke(this.setupRow.bind(this)); > > $$(''.rows'').invoke(this.setupRow).bind(this); > > $$(''.rows'').invoke(this.setupRow); > > etc, etc > > > I managed a simple workaround, but it''s using Enumerable#each instead > > of Enumerable#invoke. It isn''t the end of the world, but if it''s > > possible to use the more efficient invoke method, I''d much prefer it. > > workaround: > > $$(''.rows'').each(function(row) {this.setupRow(row);}.bind(this); > > > Some things to note. Looking at the invoke method in prototype.js, it > > uses ''this'', which refers to the page in my example. I think it''s not > > supposed to work like I''m trying to use it. But, I don''t want to jump > > to conclusions if anyone else knows differently. > > The ''this'' keyword is properly referring to my instance of myClass > > within the getRows() method. I verified this several ways, but > > probably most importantly using firebug in firefox. > > The JS error that I receive is: > > TypeError: value[method] has no properties line 708 > > prototype version: 1.6.0.1 > > If I use invoke to call a method like ''hide'' or any other global scope > > method, it works fine. > > > As I said, I have a suitable workaround, but I''m just not sure why > > invoke isn''t working like I expect it to here. Am I doing it wrong? > > Is it not supposed to work this way? Or is it a bug? > > > Thanks for any help. > > Knox--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---