0

Im currently implementing a Purchase Order type View. Where I have a PurchaseOrder table and a PurchaseOrderLine table for the items. The first thing I do when the use presses the save button I first save the Purchase Order and then I retrieve the PurchaseOrderID and save to each individual PurchaseOrder item. The problems is the following:

Promise.resolve( app.PurchaseOrder.create(formData) ).then(function(response){
                purchaseOrderID = response.collection.models[0].attributes.id;
                for(var key in formLineData){
                    if(formLineData.hasOwnProperty(key)){
                        formLineData[key]['requestID'] = purchaseOrderID;
                        app.PurchaseOrderLines.create(formLineData[key]);
                    }
                }
            }).catch(function(error){
                console.log(error);
            })

formData is the PurchaseOrder data, formLineData is the PurchaseOrderLine Data(I do the for loop to insert requestIDs to all items).

I am using a Promise because collection.fetch does not return a promise on Backbone(I think my implementation isn't quite correct because Promise.resolve() is use to make thenables a promise and in this case it isn't). The problem is that when the save button is clicked the then part passes even PurchaseOrder hasn't been created. So when it gets to the PurchaseOrderLine.create, all the items are saved without a PurchaseOrderID. I have two options:

  1. Add a server validation for this. The problem with this is that everytime is going to return an error and this can be bothersome to the user.

  2. Add a setTimeout to at least wait a couple seconds for the write to over on the server.

Could please shed a light on this topic.

Diego Gallegos
  • 1,722
  • 2
  • 18
  • 31
  • If the then() fires even when the ajax call in create isn't done, I would suggest that app.PurchaseOrder.create() does NOT return a promise. non-Promise objects are considered resolved. – billjamesdev Feb 28 '16 at 17:48
  • fetch does return a promise object. http://stackoverflow.com/questions/22967191/why-doesnt-backbone-collection-fetch-return-a-promise – Eric Guan Feb 29 '16 at 04:41
  • I did not use fetch, its **create** – Diego Gallegos Feb 29 '16 at 05:13
  • 1
    first of all you should [edit] and update your question to clarify that your issue is not with `fetch`. `create` immediately returns the created model. If you want to wait for the server till adding the created model to collection and then do something, pass `{wait: true}` – T J Feb 29 '16 at 06:52
  • Already corrected the question. – Diego Gallegos Mar 01 '16 at 14:49

1 Answers1

0

you can try something like this

app.PurchaseOrder.create(formData).then(function (response) {
  var purchaseOrderID = response.collection.models[0].attributes.id;

  return new Promise(async function (resolve) {
    for (var key in formLineData) {
      if (formLineData.hasOwnProperty(key)) {
        formLineData[key]["requestID"] = purchaseOrderID;
        await app.PurchaseOrderLines.create(formLineData[key]);
      }
    }
    resolve()
  });
});

or maybe doing something like this using Promise.all

Promise.all(formLineData.map(()=>app.PurchaseOrderLines.create(formLineData[key])))