[Dojo-interest] dojo.connect() isn't calling listener at expected time

Guillaume ORIOL go.lists at technema.fr
Mon Mar 22 11:58:06 EDT 2010


Reading eugene's blog...
Back in a few years ;-)

Le 22/03/10 16:31, Ben Hockey a écrit :
> ok - now that i paid proper attention to your original post...  i see
> that it looks like the onChange is firing but your concern seems to be
> the order in which it fires.  there is a 0ms timeout involved in
> firing the onChange (http://bugs.dojotoolkit.org/browser/tags/release-1.4.2/dijit/form/_FormWidget.js#L284
> ) and so that's why you get the order that you get.  for a really good
> explanation of how timeout affects the sequencing of your code, take a
> look at eugene's blog http://lazutkin.com/blog/2008/mar/23/javascript-edp-and-0ms-timeouts/
>
> given that the timeout is in the code, then you might have to override
> _setValueAttr either directly or via a subclass.  alternatively, you
> might try a different approach to get the sequence you want if you
> really need things to be done in sequence or maybe the sequence is ok
> but it's just not what you expected.  since i'm not sure of your
> needs, i won't try to suggest an alternative approach.
>
> hopefully that at least shows you why you're getting the results you
> get even if it doesn't tell you how to get different results.
>
> ben...
>
>
> On Mar 22, 2010, at 11:11 AM, Guillaume ORIOL wrote:
>
>> Thank you Ben, but none of the two solutions you suggest solve my
>> problem.
>> The sequence remains the same.
>> Any idea?
>>
>> Le 22/03/10 15:51, Ben Hockey a écrit :
>>> you don't need to override the function.  that widget (and other form
>>> value widgets) has some conditions which need to be true for it to
>>> fire onChange.
>>>
>>> there are 2 ways you can do this with the NumberTextBox:
>>> 	- set the intermediateChanges property to true
>>> 	- pass an additional parameter when changing the value
>>> programmatically
>>> 		heightElement.attr('value', height, true);
>>>
>>> i didn't spend a lot of time looking but i couldn't find docs to
>>> clarify this for you.  i just figured this out from taking a quick
>>> look at the code so hopefully that works.
>>>
>>> ben...
>>>
>>>
>>> On Mar 22, 2010, at 10:30 AM, Guillaume ORIOL wrote:
>>>
>>>> Both are Dijit of class dijit.form.NumberTextBox
>>>> Do I have to override this function in dijit.form.NumberTextBox?
>>>>
>>>> Le 22/03/10 15:00, Ben Hockey a écrit :
>>>>> what kind of objects/widgets are heightElement and widthElement?
>>>>> have
>>>>> they been written so that changing their value will call onChange?
>>>>> they would need some code which does something similar to this:
>>>>>
>>>>> _setValueAttr: function(value){
>>>>> 	this.value = value;
>>>>> 	this.onChange&&    this.onChange(value);
>>>>> }
>>>>>
>>>>>
>>>>>
>>>>> ben...
>>>>>
>>>>>
>>>>> On Mar 22, 2010, at 9:38 AM, Guillaume ORIOL wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I am facing a problem with dojo.connect(). Considering the
>>>>>> following
>>>>>> code, I don't understand why
>>>>>> the listener function "dimensionTextboxChanged" is not called at
>>>>>> expected time.
>>>>>>
>>>>>> dojo.addOnLoad(function() {
>>>>>>      var dimensionElement = dijit.byId("main-dimension");
>>>>>>      var heightElement = dijit.byId("main-height");
>>>>>>      var widthElement = dijit.byId("main-width");
>>>>>>
>>>>>>      var dimensionSelectChanged = function() {
>>>>>>          console.log("dimensionSelectChanged entered");
>>>>>>          if (dimensionChangedByProgram) {
>>>>>>              return;
>>>>>>          }
>>>>>>
>>>>>>          var dimension = this.value;
>>>>>>          if (dimension.length>    0) {
>>>>>>              var h = null, width = null;
>>>>>>              dimensionStore.fetchItemByIdentity({
>>>>>>                  identity: dimension,
>>>>>>                  onItem: function(item) {
>>>>>>                      height =
>>>>>> parseFloat(dimensionStore.getValue(item, "height"));
>>>>>>                      width =
>>>>>> parseFloat(dimensionStore.getValue(item,
>>>>>> "width"));
>>>>>>                  }
>>>>>>              });
>>>>>>
>>>>>>              dimensionChangedByProgram = true;
>>>>>>              console.log("dimensionSelectChanged 1");
>>>>>>              checkOrientationBoxes(height, width);
>>>>>>              console.log("dimensionSelectChanged 2");
>>>>>>              heightElement.attr("value", height);
>>>>>>              console.log("dimensionSelectChanged 3");
>>>>>>              widthElement.attr("value", width);
>>>>>>              console.log("dimensionSelectChanged 4");
>>>>>>              dimensionChangedByProgram = false;
>>>>>>           }
>>>>>>      };
>>>>>>
>>>>>>      var dimensionTextboxChanged = function() {
>>>>>>          console.log("dimensionTextboxChanged entered");
>>>>>> 	...
>>>>>>      };
>>>>>>
>>>>>>      dojo.connect(dimensionElement, "onChange",
>>>>>> dimensionSelectChanged);
>>>>>>      dojo.connect(heightElement, "onChange",
>>>>>> dimensionTextboxChanged);
>>>>>>      dojo.connect(widthElement, "onChange",
>>>>>> dimensionTextboxChanged);
>>>>>> });
>>>>>>
>>>>>> I get this result in Firebug:
>>>>>> dimensionSelectChanged entered
>>>>>> dimensionSelectChanged 1
>>>>>> dimensionSelectChanged 2
>>>>>> dimensionSelectChanged 3
>>>>>> dimensionSelectChanged 4
>>>>>> dimensionTextboxChanged entered
>>>>>> dimensionTextboxChanged entered
>>>>>>
>>>>>> But I was expecting:
>>>>>> dimensionSelectChanged entered
>>>>>> dimensionSelectChanged 1
>>>>>> dimensionSelectChanged 2
>>>>>> dimensionTextboxChanged entered
>>>>>> dimensionSelectChanged 3
>>>>>> dimensionTextboxChanged entered
>>>>>> dimensionSelectChanged 4
>>>>>> (as heightElement.attr("value", height) changes the
>>>>>> heightElement).
>>>>>>
>>>>>> Best regards,
>>>>>> --
>>>>>> Guillaume ORIOL
>>>>>> Software engineer
>>>>>> Technema
>>>>>> _______________________________________________
>>>>>> FAQ: http://dojotoolkit.org/support/faq
>>>>>> Book: http://docs.dojocampus.org
>>>>>> Dojo-interest at mail.dojotoolkit.org
>>>>>> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


More information about the Dojo-interest mailing list