0leg
0leg

Reputation: 14134

Dojo: widget instances share the same variable

There is a custom Foo widget defined, which has local `bar' value. And an onClick function that changes 'bar' to something.

Now, multiple instances of Foo are defined declarative inside an HTML-template like this:

<input id="foo1" data-dojo-type="path-to-wiget/Foo">
<input id="foo2" data-dojo-type="path-to-wiget/Foo">
<input id="foo3" data-dojo-type="path-to-wiget/Foo">

The problem is that each of those inputs should have its own instance of foo variable, but for some reason they behave like there only one!

>> clicking foo1 -> foo1.foo = 'bar'
>> clicking foo2 -> foo2.foo = 'baz'.. but foo1.foo is set to 'baz' as well.. why?

How to make sure, each widget has its own foo variable?

Upvotes: 1

Views: 158

Answers (1)

GibboK
GibboK

Reputation: 73908

In your question you have not added your code for Foo.js so it is difficult to understand your issue.

But if you are going for a declarative approach data-dojo-type you could consider passing your property per instance using data-dojo-props.

In the following example, you pass the value of myProp in your HTML markup, you can see that is being added to the property instance when you retrieve it using dijit/registry.

Example, please open console:

https://jsfiddle.net/e5dx8nue/

<input id="foo1" data-dojo-type="dijit/form/Button" data-dojo-props="myProp: 'foo1'">
<input id="foo2" data-dojo-type="dijit/form/Button" data-dojo-props="myProp: 'foo2'">
<input id="foo3" data-dojo-type="dijit/form/Button" data-dojo-props="myProp: 'foo3'">

require(['dojo/parser', 'dijit/registry'], function(parser, registry) {
  parser.parse().then(function() {
    var widget1 = registry.byId('foo1'); // print foo1
    var widget2 = registry.byId('foo2'); // print foo2
    var widget3 = registry.byId('foo3'); // print foo3
    console.log(widget1.myProp);
    console.log(widget2.myProp);
    console.log(widget3.myProp);
  });
});

More information on declarative syntax can be found here.

Upvotes: 2

Related Questions