Hi,
Your options object is common to all instances of Foo, because you
declared it that way:
> [snip]
> Foo = Class.create({
>
> options: $H({'onSelect' : false}),
>
> initialize: function(element, options) {
> [snip]
'options', just like 'initialize', is shared by all of your
instances. (You defined them exactly the same way, after all.) If
you want instance-specific properties, set them up in your contructor
as you do with the 'el' property.
HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available
On Mar 10, 11:34 am, Mathieu <[email protected]> wrote:
> Hello,
>
> I just had a bug the morning, I still dont understand why it did not
> work
>
> I have two classes,
> I call Foo.select on element click.
> Foo.select call Bar.action
>
> When creating multiple Bar, the Bar.action call is always the last
> instanciated function...
>
> Ok that's not very clear, but imagin your creating three Bar instance:
>
> Foo = Class.create({
>
> options: $H({'onSelect' : false}),
>
> initialize: function(element, options) {
> this.el = $(element);
> this.el.observe('click', this.select.bind(this));
> this.options.update(options);
> },
>
> select: function() {
> if(this.options.get('onSelect')) {
> this.options.get('onSelect')();}
> }
> });
>
> Bar = Class.create({
>
> initialize: function(element) {
> this.el = $(element);
> new Foo(this.el, {'onSelect':
> this.action.bind(this)});
> },
>
> action: function() {
> alert(this.el.inspect());
> }
> });
>
> => It does not work, the action function will always alert the last
> Bar instance element :(
>
> Oddly, this works:
>
> Foo = Class.create({
>
> initialize: function(element) {
> this.el = $(element);
> this.el.observe('click', this.select.bind(this));
> this.options.update(options);
> },
>
> select: function() {
> if(this.onSelect) { this.onSelect();}
> }
> });
>
> Bar = Class.create({
>
> initialize: function(element) {
> this.el = $(element);
> var foo = new Foo(this.el);
> foo.onSelect = this.action.bind(this);
> },
>
> action: function() {
> alert(this.el.inspect());
> }
> });
>
> That's not a big deal because now I found a way to make my code work.
> But I'd like to understand why the first bit of code did not work.
> Is it related to the use of the Hash ?
>
> It is as if there where a misunderstanding between Bar Instance and
> Bar Class
>
> Thanx for your help
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---