registerAdapter: hasMany relationships with third parties


#1

Trying to sort out how to create a new data adapter for a third party rest server. Right now I can easily findAll, but can’t manage to set it up to bind that response to a current user.

I’m pretty sure I need to set up a hasMany call, but currently the hasMany relationship for all Users is empty… How can I set up that initial binding?

Another complication, is I’d like to be able to send the request to the third party server with data from the current User model, but I’m not sure how to access that from within the adapter itself. I’m currently using an Auth object that has that information, but it’s not ideal.

Models:

App.User = DS.Model.extend
  workauths: DS.hasMany 'App.Workauth',
    inverse: 'worker'

App.Workauth = DS.Model.extend
  worker: DS.belongsTo 'App.User'

Router:

App.Router.map ->
  @resource 'users', ->
    @resource 'user',
      path: '/:user_id', ->
        @resource 'workauths'

Store:

App.Store.registerAdapter App.Workauth, DS.GUTSAdapter.extend
  url: [redacted]

Adapter:

DS.GUTSAdapter = DS.Adapter.extend Ember.Evented,
  findAll: (store, type) ->
    return if !App.Auth.get('user').get('ldap')
    method = 'get_work_auths_json'
    adapter = @
    token = App.Auth.get('user').get('gutsToken')
    ldap = App.Auth.get('user').get('ldap')
    url = this.url
    $.ajax(
      url: url+method, 
      data: 
        ldap_username: ldap,
        ldap_auth_token: token,
      type: 'post'
    ).done( (data) ->

      #model mapping and checking for uniqueness by id.

      workauths=( \
          id:workauth.work_auth_id, \
          name: ( \
            if Em.isEmpty workauth.work_auth_name \
            then workauth.project_name \
            else workauth.work_auth_name), \
          hours:workauth.work_auth_hours, \
          due:workauth.work_auth_date_due, \
          worker: App.Auth.get('userId') \
          for workauth in data).unique 'id'
      workauths = 
        workauths : workauths
      adapter.didFindAll store, type, workauths