[Proto-Scripty] Re: Is this proper use of bind?

2009-07-20 Thread ColinFine


 Let's look at the method checkAll in Contact object:

 var Contact = {
     initialize: function(json,id_contact){

 $('check-all').observe('click',this.checkAll.bindAsEventListener(this));
     },
     checkAll: function(e) {
         e.stop();
         this.contactChecked = [];

         $$('#contacts-ul .contact-checkbox').each(function(c){
                 c.checked = true;
                 Contact.contactChecked.push(parseInt(c.value));

 Here 'this' is a window object so I could use bind(this) and then use
 this.contactChecked,
 but I can skip bind and use Contact.contactChecked which I often do.

The behaviour depends on whether you call the constructor with 'new'
or not (another of the features that Crockford describes as 'very
bad').
If you call it as 'new Contact', than inside the 'initialize', 'this'
refers to the new Contact object you are creating, and checkAll will
be bound so that inside it 'this' also refers to that Contact object.

If you call the constructor without 'new', then 'this' refers to the
global object, and that is what will be bound as 'this' inside
'checkAll'.

So yes, for this sort of use you usually do need 'bind'. You very
rarely need 'bindAsEventListener', and I cannot remember what the
special case is that it is designed for.


--~--~-~--~~~---~--~~
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: Is this proper use of bind?

2009-07-16 Thread ColinFine



On Jul 15, 9:41 am, keemor kee...@gmail.com wrote:
 Thanks for this tip, I didnt know that

 What about this fragment:
  xhrRequestFake: function(e){
                //now this is a li which I want to use to read id
 from
                 itemManager.json = [{'key':'no'},{'key':'way!'}];
                 itemManager.createHTML();

 }

 Is it a good practice to call method like this itemManager.createHTML
 (); instead of  this.createHTML(); if I want to preserve 'this' as a
 context of element from the event?


I'm not quite sure what you are asking.
Inside a function 'this' always refers to the global object (the
window) unless you have taken steps to make it refer to something
else. A common way to make it refer to something else is by calling
the function as a method (your 'itemManager.createHTML()'). Prototype
provides another way in Function.bind.

So if you use ItemManager.createHTML(), inside 'createHTML', 'this'
will refer to ItemManager.  But as far as I can see, you are not using
'this' inside 'createHTML' anyway, so the question is irrelevant.

--~--~-~--~~~---~--~~
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: Is this proper use of bind?

2009-07-16 Thread Romek Szwarc
2009/7/16 ColinFine colin.f...@pace.com



 I'm not quite sure what you are asking.
 Inside a function 'this' always refers to the global object (the
 window) unless you have taken steps to make it refer to something
 else. A common way to make it refer to something else is by calling
 the function as a method (your 'itemManager.createHTML()'). Prototype
 provides another way in Function.bind.

 So if you use ItemManager.createHTML(), inside 'createHTML', 'this'
 will refer to ItemManager.  But as far as I can see, you are not using
 'this' inside 'createHTML' anyway, so the question is irrelevant.


Let's look at the method checkAll in Contact object:

var Contact = {
initialize: function(json,id_contact){

$('check-all').observe('click',this.checkAll.bindAsEventListener(this));
},
checkAll: function(e) {
e.stop();
this.contactChecked = [];

$$('#contacts-ul .contact-checkbox').each(function(c){
c.checked = true;
Contact.contactChecked.push(parseInt(c.value));



Here 'this' is a window object so I could use bind(this) and then use
this.contactChecked,
but I can skip bind and use Contact.contactChecked which I often do.

I would like to know how you deal with such situation, because I would use
bind as rarely as possible, that's all.


 });
}


-- 
keemor

--~--~-~--~~~---~--~~
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: Is this proper use of bind?

2009-07-15 Thread keemor

Thanks for this tip, I didnt know that

What about this fragment:
 xhrRequestFake: function(e){
   //now this is a li which I want to use to read id
from
itemManager.json = [{'key':'no'},{'key':'way!'}];
itemManager.createHTML();
}

Is it a good practice to call method like this itemManager.createHTML
(); instead of  this.createHTML(); if I want to preserve 'this' as a
context of element from the event?


On 14 Lip, 19:16, mr_justin gro...@jperkins.otherinbox.com wrote:
                  this.createHTML = this.createHTML.bind(this);
                  this.createHTML();

 There is no need to do that. Just use this.createHTML() straight away.
 The instance is already in the correct scope.
--~--~-~--~~~---~--~~
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: Is this proper use of bind?

2009-07-14 Thread Lox


Regarding your first exemple I would:

$$('#list  li').invoke
('observe','click',this.xhrRequestFake.bindAsEventListener(this) );

then in xhrRequestFake function:

li = e.element();

http://www.prototypejs.org/api/event
http://www.prototypejs.org/api/function/bindaseventlistener

--~--~-~--~~~---~--~~
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: Is this proper use of bind?

2009-07-14 Thread keemor

Yes, but if there were lisomething span class=texttext/span
strongsomething else/strong /li

e.element() would be li, span or strong and I would rather use
e.findElement('li')

But I don't really know the advantage of such solution.

On 14 Lip, 16:20, Lox gecka.comp...@gmail.com wrote:
 Regarding your first exemple I would:

 $$('#list  li').invoke
 ('observe','click',this.xhrRequestFake.bindAsEventListener(this) );

 then in xhrRequestFake function:

 li = e.element();

 http://www.prototypejs.org/api/eventhttp://www.prototypejs.org/api/function/bindaseventlistener




--~--~-~--~~~---~--~~
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: Is this proper use of bind?

2009-07-14 Thread Lox

 But I don't really know the advantage of such solution.

Well as you  wanted to have access in method to the clicked element
and object in
the same time. that is what you will get.
--~--~-~--~~~---~--~~
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: Is this proper use of bind?

2009-07-14 Thread mr_justin

 this.createHTML = this.createHTML.bind(this);
 this.createHTML();

There is no need to do that. Just use this.createHTML() straight away.
The instance is already in the correct scope.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---