 
 
   retrieveData : ->
-       
+
     ajaxOptions =
       url : @pageSelectionDiv.data("url")
       dataType : "json"
       type : "get"
 
-    $.ajax(ajaxOptions).then(@handleData)
+    @dataRetrievalPromise = $.ajax(ajaxOptions)
+    @dataRetrievalPromise.then(@handleData)
 
   handleData: (responseData) =>
-    
+
     @allElements = @elementsToShow = responseData.data
 
     pageCount = Math.ceil(@allElements.length / @rowsPerPage)
-    
+
     pageSelectionHandler = (event, number) =>
       index = number - 1
       json = @getElementsForPage(index)
       @displayJSON(json)
-    
+
     @pageSelectionDiv.bootpag(
       total: pageCount
       page: 1

    contains = false

    $.each data, (key, value) =>
    
      if _.isObject(value) or _.isArray(value)
         contains = @JSONcontains value, query
         return !contains # if contains then break else continue



  getElementsForPage : (index) ->

    start = @rowsPerPage * index
    return @elementsToShow.slice(start, start + @rowsPerPage)


  updatePageCount : ->

    @pageSelectionDiv.bootpag(
      total: Math.ceil(@elementsToShow.length / @rowsPerPage)
      page:  1
