marat-gainullin / platypus-js

Apache License 2.0
9 stars 9 forks source link

model.requery() call until finishing last one in HTML5 client #84

Closed valeriy-maslov closed 8 years ago

valeriy-maslov commented 8 years ago

Recently I've noticed that in html5 client model.requery() acting strange working with global API (could be with AMD too). It took some time to find a reason and a way to reproduce this kinda behaviour. So I try to explain the way I get it. Please, check this out.

E.g. we have html5 client module StuckModule working in browser. There is a model with single query in it: model.example. And it has element class ECItem.

var model = P.loadModel(this.constructor.name);
model.example.elementClass = ECItem;

Also ECItem has it's own model inside and some methods using it.

function ECItem() {
   model = P.loadModel(this.constructor.name);
   var v1,v2,v3;
   this.something = function(callback) {
      if (v1) {
         callback(v1);
      } else {
         model.q1.params.somevalue = 123;
         model.q1.requery(function() {
            v1 = model.q1[0];
            callback(v1);
         },function(e) {P.Logger.warning(e);});
      }
   };
}

So let's assume that there is some process in StuckModule which making something with data and calls method something() of the single instance of ECItem several times, e.g. to set calculated values to GUI widgets, it's not important right now.

//some calculations
model.example[0].something(function(v) {
   form.someLabel.text = v.someString;
});
//...more calculations and another call
model.example[0].something(function(v) {
   form.maybeTextEdit.text = v.anotherString + someCalcedValue;
});
//... etc....

Please note that all this calls made from one method in StuckModule. So the thing is that second call of something() made until requery() inside the first call did not finished yet, so v1 still has not any data and second call also goes into requery() branch. But as I can undestand Platypus prevents double requery() calling in same context, so success callback of requery works only on first call and never in second, third etc.

The question: Is there any way to make it possible to call requery() of the same model several times (including calls with different params) so all appropriate callbacks worked nicely? Is that a bug or something anyway?

marat-gainullin commented 8 years ago

Platypus.js cancels prior calls to model.requery(). So only one callback (the last one) will be called. Take a look on model.entity.query(parameters, function(aData){...}. It allows simultaneous calls and provides its callback with different results, according to parameters argument.