0

I'm going round in circles and can't seem to figure out a solution from the resources currently available here on Stack or Google. There's got to be something obvious that I'm missing, perhaps you might be able to help?

Story summary:

  • A javascript function launches when clicked and creates a new contact in our database.
  • Additional functions are then called upon successful creation to toggle some settings where necessary, dependant on a few checkboxes.
  • Calls are currently being made asynchronously, resulting in only the last function call to successfully update the contact.
  • I can't, for the life of me, get the call to work one after the other instead.
  • Each call returns a JsonResult upon successful completion, if that helps at all (needed for other areas of the application.

Code currently looks like: function CreateClicked(){ Contact.Create(**bunch of params**, function(data){ if(data.success) { togglePrimary(data.newId); toggleBilling(data.newId); toggleTechnical(data.newId); toggleBalance(data.newId); toggleSecurity(data.newId); toggleMarketing(data.newId); Modal.Load(**loads a modal view**); } } }

The toggle functions then look like: function togglePrimary(id) { if ($("#contact_admin_primaryrole").prop('checked')) {Contact.TogglePrimaryRole(id);} }

Which calls a controller function that looks like this:

public JsonResult TogglePrimaryRole(int contactId){ try{ var c = new Contact(contactId); c.IsPrimaryContact = !c.IsPrimaryContact; c.Update(AuthenticatedUser.Username, !c.IsPrimaryContact); return Json(JSONResponseFactory.SuccessResponse("Contact updated successfully")); } catch (Exception ex){ return Json(JSONResponseFactory.ErrorResponse(ex.Message)); } }

How should I go about setting this up so that each toggle function doesn't start until the previous one has finished and returned a Json response, regardless of success?

Any ideas?

Cheers, Dez

2 Answers 2

1

Using jQuery promises should help:

togglePrimary(data.newId).then(toggleBilling(data.newId)).then(toggleTechnical(data.newId)

etc.

This will run the next function only if the last one was a success. If you want to call the function irrelevent of the outcome then use always() instead of then()

togglePrimary(data.newId).always(toggleBilling(data.newId)).always(toggleTechnical(data.newId)

This will require jquery 1.6 or higher to be referenced. To reference from the CDN add the following

<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion. Trying both ways now executes the first function correctly, but then I get the following error on the rest of them: "Uncaught TypeError: Cannot read property 'always' of undefined"
Do you have Jquery referenced? Updated my answer accordingly
Yep, jQuery is referenced in the document, but really doesn't seem to like either this() or always() methods. :/
0

I couldn't seem to get anywhere with promises, or javascript function chaining with callbacks... so I turned the values of each status into an array of strings and parsed it within the controller instead!

Thanks for helping :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.