     url: "/countries"
     model: Country
   )
+
+  Occupations = Backbone.Collection.extend url: "/occupations"
+
   Relationships = Backbone.Collection.extend(
     url: ->
       "/relationships/" + @models[0].id
 
     model: Relationship
   )
-  people = new PaginatedPeople() # create a global collection of all people
+
   PeopleListView = Backbone.View.extend(
     el: "#page"
     initialize: ->
+      that = @
       @$el.html $("#view-loading").html()
-      that = this
-
-      people.getFirstPage 
-          dataType: "jsonp"
-        success: (result) ->
-
-        that.render result
-        return
-      return
+      people = new PaginatedPeople()
+      $.when people.getFirstPage 
+        dataType: "jsonp"
+        error: (args) ->
+            console.log arguments
+      .done ->
+        that.render(people)
+
     render: (people) ->
       that = @
       breadcrumb_template = _.template($("#breadcrumb-template").html(),

      return
  )

  PeopleSearchResultsView = Backbone.View.extend(
    el: "#page",
    render: (querystring) ->
      that = @
      peopleResults = new PeopleSearch
      peopleResults.fetch
        #dataType: "jsonp"
        url: "/peoplesearch?" + querystring
        success: (people) ->

          breadcrumb_template = _.template($("#breadcrumb-template").html(),
            breadcrumbs: [{"Search"},"Results - " + people.models.length]
          )
          $("#breadcrumb").html breadcrumb_template
          that.$el.empty()

          require [
            "backgrid"
          ], (Backgrid) ->
            peopleGrid = new Backgrid.Grid(
              columns: [
                {
                  name: "surname"
                  cell: "string"
                  sortable: true
                  editable: false
                }
                {
                  name: "forenames"
                  cell: "string"
                  sortable: true
                  editable: false
                }
                {
                  name: "sex"
                  label: "gender"
                  cell: "string"
                  sortable: true
                  editable: false
                }
                {
                  name: "id"
                  label: "URL"
                  cell: Backgrid.UriCell.extend(events:
                    click: "viewPerson"
                  )
                  sortable: false
                  editable: false
                }
              ]
              collection: people
            )
            that.$el.append peopleGrid.render().el

  )


    return

  router.on "route:viewPersonResults", (querystring) ->
    peopleSearchResultsView = new PeopleSearchResultsView()
    peopleSearchResultsView.search querystring
    return


