completing an ajax call in onBeforeApplyEdits prior to applyEdits happening

Discussion created by StevenHas on Jan 25, 2013
Latest reply on Feb 4, 2013 by StevenHas
Hi there,

I'm attempting to set an id value on any inserts prior to transmission to the server.

I'm listening to the onBeforeApplyEdits event and attempting to complete an ajax call for the next ID value prior to applyEdits happening. The inspector is showing the values after completion but applyEvent is not sending them to the database.

I think I'm not understanding callbacks properly, can anyone point me in the right direction to holding up the applyEvent until my call is complete or trouble shoot my code below?

Thanks very much!

My code is in a backbone framework but is essentially as follows.

Listens for event on the layer, fires the backbone event listened for elsewhere in the application
dojo.connect(treeEditLayer, 'onBeforeApplyEdits', function(a,u,d){
  console.log('onBeforeApplyEdits called');
  return _this.trigger('BBonBeforeApplyEdits',a,u,d);

responds to the backbone event sending the update via the fetchNextId function for an insert.
treeEditView.on("BBonBeforeApplyEdits", function (adds, updates, deletes){
  if (adds)
    flEditHelper.fetchNextId (adds, updates, deletes);  // ajax call to next id, which then calls onBeforeApplyEdits on its succes
    // This will update the inspector but not make it to the database
    flEditHelper.onBeforeApplyEdits(adds, updates, deletes); //pass straight to update  - this works fine!

in flEditHelper fetchNextId function
fetchNextId: function(a,u,d){
  var tableName = "CITY_TREES";
  var this2 = this;
  var nextId;

  this.xhr = $.ajax({
    url: urlRequest, type: "GET", dataType: "xml", timeout: 5000,
    success: function (webResults) {
      console.log('start success - fetchNextId : ' + new Date().getTime());

      nextId = webResults;
      this2.onBeforeApplyEdits(a,u,d,nextId); // then calls onBeforeApplyEdits

      console.log('end success - fetchNextId : ' + new Date().getTime());
    error: function (ex, ey, ez){
    complete: function (x){
      console.log("complete: " + x);

in flEditHelper onBeforeApplyEdits function
// onBeforeApplyEdits function
onBeforeApplyEdits: function (adds, updates, deletes, nextId){
  console.log('start - onBeforeApplyEdits : ' + new Date().getTime());
  if (adds)
      adds[0].attributes['UNITID'] = nextId;
      adds[0].attributes['GIS_EDITOR'] =[0].userId;
  if (updates)
    updates[0].attributes['GIS_EDITOR'] =[0].userId;
  console.log('end - onBeforeApplyEdits : ' + new Date().getTime());