Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As all your functions have similar AJAX setup options I would recommend you to setup common options globally using the <a href="http://api.jquery.com/jQuery.ajaxSetup/" rel="nofollow noreferrer"><code>$.ajaxSetup</code></a> function:</p> <pre><code>$(function() { $.ajaxSetup({ contentType: 'application/json; charset=utf-8', global: false, async: false, dataType: 'json', beforeSend: function() { $('.loading').show(); }, complete: function() { $('.loading').hide(); }, }); }); </code></pre> <p>Next let's first consider the <code>getClients</code> and <code>getDrivers</code> functions. Those look pretty much the same. The only difference I can see between them is the url you are sending the AJAX request and the html that's being appended to the <code>#ResultsDiv</code>. So you could have two separate functions that handle the result:</p> <pre><code>function formatDriversResult(result) { return '&lt;div class="resultsdiv"&gt;&lt;br /&gt;&lt;span style="display: inline-block;width:220px;" class="resultName"&gt;' + result.DriverName + '&lt;/span&gt;&lt;span class="resultfields" style="padding-left:10px;"&gt;Mobile No&amp;nbsp;:&lt;/span&gt;&amp;nbsp;&lt;span class="resultfieldvalues"&gt;' + result.DriverMobileNo + '&lt;/span&gt;&lt;span style="float:right; padding-right:2px;"&gt;&lt;input type="checkbox" value=' + result.DriverId + ' onclick="storeIds();"/&gt;&lt;/span&gt;&lt;/div&gt;'; } </code></pre> <p>and</p> <pre><code>function formatClientsResult(result) { return '&lt;div class="resultsdiv"&gt;&lt;br /&gt;&lt;span style="display: inline-block;width:220px;" class="resultName"&gt;' + result.ClientName + '&lt;/span&gt;&lt;span class="resultfields" style="padding-left:10px;"&gt;Mobile No&amp;nbsp;:&lt;/span&gt;&amp;nbsp;&lt;span class="resultfieldvalues"&gt;' + result.ClientMobNo + '&lt;/span&gt;&lt;span style="float:right; padding-right:2px;"&gt;&lt;input type="checkbox" value=' + result.ClientId + ' onclick="storeIds();"/&gt;&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span class="resultfields"&gt;Address&amp;nbsp;:&lt;/span&gt;&amp;nbsp;&lt;span class="resultfieldvalues"&gt;' + result.ClientAddress + '&lt;/span&gt;&lt;/div&gt;'; } </code></pre> <p>And finally the two functions could be merged into a single one:</p> <pre><code>function getEntities(url, currentPage, formatResultFunction) { $.ajax({ url: url, data: { 'currentPage': (currentPage + 1), 'pageSize': 5 }, success: function(data) { if (data.isRedirect &amp;&amp; data.isRedirect === true) { alert('must redirect to ' + data.redirectUrl); location = 'http://www.google.com'; } else { var divs = ''; $("#hfId").val(''); $("#ResultsDiv").empty(); $.each(data.Results, function() { divs += formatResultFunction(this); }); $("#ResultsDiv").append(divs); $(".resultsdiv:even").addClass("resultseven"); $(".resultsdiv").hover(function() { $(this).addClass("resultshover"); }, function() { $(this).removeClass("resultshover"); }); $("#HfId").val(""); $("#HfId").val(data.Count); } } }); return false; } </code></pre> <p>Now when you want to get the current clients:</p> <pre><code>var clients = getEntities("Clients/GetClients", currentPage, formatClientsResult); </code></pre> <p>and when you want to get the current drivers:</p> <pre><code>var drivers = getEntities("Staff/GetDrivers", currentPage, formatDriversResult); </code></pre> <hr> <p>Next let's consider the <code>getClientbyId</code> and <code>getDriverById</code> functions. They could be simplified to:</p> <pre><code>function getEntityById(data, url, formatResultFunction) { $.ajax({ url: url, data: data, success: function(data) { formatResultFunction(data); $("#adddiv").show(); $("#ResultsDiv").hide(); $("#PagerDown").hide(); $("#ImageButtonDiv").hide(); } }); return false; } </code></pre> <p>The same pattern could be used for the rest.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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