         return note
       else
         console.log('Local note fetch missed, fetch from remote: ' + guid)
-        return apiClient.notes.note({id: guid}).$promise.then (data) =>
+        return apiClient.get_note(guid).then (note) =>
           _note =
             guid: guid
-            title: data.note.title
-            md: data.note.md
+            title: note.title
+            md: note.md
             status: @NOTE_STATUS.SYNCED_ALL
-            resources: data.note.resources
+            resources: note.resources
           return @update_note(_note)
 
   # ------------------------------------------------------------

  # ------------------------------------------------------------
  load_remote_notes: =>
    # TODO handle failure
    return apiClient.notes.all().$promise.then (data) =>
      @_merge_remote_notes(data['notes']).then(
        () =>
          console.log('finish merging remote notes')
          return @get_all_notes()

# ----------------------------------------------------------------------------------------------------------------------
# Service: apiClient
# ----------------------------------------------------------------------------------------------------------------------
markever.factory 'apiClient', ['$resource', ($resource) -> new class APIClient
  note_resource: $resource('/api/v1/notes/:id', {id: '@id'}, {
    all: {method : 'GET', params : {id: ''}},
    note: {method : 'GET', params: {}},
    newest: {method : 'GET', params : {id: 'newest'}},
    save: {method : 'POST', params: {id: ''}},
  })

  # return promise with all notes
  get_all_notes: () =>
    return @note_resource.all().$promise.then (data) =>
      return data['notes']

  # return promise with the note
  get_note: (guid) =>
    return @note_resource.note({id: guid}).$promise.then (data) =>
      return data.note

  # return promise with saved note that is returned from remote
  save_note: (guid, title, enml) =>
    _post_data = {
      guid: guid
      title: title
      enml: enml
    }
    return @note_resource.save(_post_data).$promise.then (data) =>
      return data.note
]

# ----------------------------------------------------------------------------------------------------------------------
