title: Accepted use methods of javascript callbacks
body: <p>I am looking for advice to ensure that I am using callbacks and javascript coding using generally accepted js guidelines.  What is listed below is two functions which are chained together.  Basically its a list of checks which need to be completed prior to creating the entity.  I don't expect the final version to use a ajax POST but it is a good way to test all of the error handling.</p>
  
  <p>Advice or recommendations would be appreciated!!  I will give credit to the best explained and critiqued answer.</p>
  
  <pre><code>function relationship_check(app_label, model, company_id, params, form, callback_function){
      // This will check to see if a relationship exists.  This works even on new objects.
      kwargs = $.extend({}, params);
      kwargs['app_label'] = app_label;
      kwargs['model'] = model;
      kwargs['relationship__company'] = company_id;
      kwargs['error_on_objects_exists_and_no_relation'] = true;
      ajax_req = $.ajax({
          url: "{% url 'api_get_discover' api_name='v1' resource_name='relationship' %}",
          type: "GET",
          data: kwargs,
          success: function(data, textStatus, jqXHR) {
              callback_function(form, params)
          },
          error: function(data, textStatus, jqXHR) {
              results = $.parseJSON(data.responseText)
              if (results['object_exists'] &amp;&amp; ! results['relationships_exists']){
                  django_message(results['create_string'], "info");
              } else {
                  django_message(results['error'], "error");
              }
              return false
          }
      })
      return false
  };
  
  function create_community(form, data){
      var self = $(this),
      ajax_req = $.ajax({
          url: self.attr("action"),
          type: "POST",
          data: data,
          success: function(data, textStatus, jqXHR) {
              django_message("Saved successfully.", "success");
          },
          error: function(data, textStatus, jqXHR) {
              var errors = $.parseJSON(data.responseText);
              $.each(errors, function(index, value) {
                  if (index === "__all__") {
                      console.log(index + " : " + value )
                      django_message(value[0], "error");
                  } else {
                      console.log(index + " : " + value )
                      apply_form_field_error(index, value);
                  }
              });
          }
      });
  }
  
  $(document).on("submit", "#community_form", function(e) {
      e.preventDefault();
      clear_form_field_errors("#community_form");
      var data = {
          name: $(this).find("#id_name").val(),
          city: $(this).find("#id_city").val(),
          cross_roads: $(this).find("#id_cross_roads").val(),
          website: $(this).find("#id_website").val(),
          latitude: $(this).find("#id_latitude").val(),
          longitude: $(this).find("#id_longitude").val(),
          confirmed_address: $(this).find("#id_confirmed_address").val()
       };
      console.log(data)
      relationship_check(
              'community', 'community', '{{ request.user.company.id }}',
              data, "#community_form", create_community);
  });
  </code></pre>