Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone custom method
    primarykey
    data
    text
    <p>I'm building a shopping cart with Backbone and I'm having a hard time determining how to structure the interaction with the server. When the user hits the checkout (and after the validation) I need to:</p> <ol> <li><p>Save the Order using order.save() - Because the order was created on the client this will hit the route on the server and give me back an ID. I need the ID before I process the order.</p></li> <li><p>On success I call a custom method on the Order which hits the server again which does a request to the card processing service.</p></li> <li><p>I either report the err to the client and let him modify and retry or I update the order record and create a Payment record in the database.</p></li> </ol> <p>Here is my processOrder method in the Order Model:</p> <pre><code>processOrder: function() { var order = this.toJSON(); $.ajax({ url: '/api/order/process', type: 'POST', data: order, dataType: 'json', success: function(res) { console.log(res); }, error: function(err) { //show error message console.log('error: ' + JSON.stringify(err)); } }); } </code></pre> <p>This is where the form is submitted in my view</p> <pre><code>saveOrder: function() { ... var order = this.order; order.set(this.frmJSON); order.save(this.frmJSON, { success: function(res) { console.log('order saved...'); order.processOrder(); }, error: function(err) { console.log(JSON.stringify(err)); } }); } </code></pre> <p>The order is saved on the server and the Order gets an ID inserted into it. When I call processOrder it works until it gets to the ajax call. It never hits the server and the browser hangs up with no error. Is there a better way to structure these steps?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload