Proper way to load up the current user's data from preloaded JSON

Hi there!

I’m having some trouble setting something up that I feel should be easy, but I think I’m missing something.

What I have:

  • My ember app is only available if the user is logged in (Rails app)
  • Active model serializers can dump model data onto the HTML page in the proper form for Ember-data
  • I am loading the current user’s personal data (name/email) via an initializer in the ember-app. Looks like this:
Ember.Application.initializer
  name: 'currentUser'

  initialize: (container) ->
    store = container.lookup('store:main')
    preloadJSON = JSON.parse $('meta[name="current-user"]').attr('content')

    if preloadJSON
      loadedObj = store.push('currentUser', currentUser)
      user = store.find('user', loadedObj.id)
      
      controller = container.lookup('controller:currentUser').set('content', user)
      container.typeInjection('controller', 'currentUser', 'controller:currentUser')

What I’d like to accomplish:

  • Have some models scoped specifically to this user (i.e. the user has_one questionnaire) also be preloaded into the store, so that in my ember code I can call currentUser.questionnaire straight off the bat without a call to the server.

I’m not quite sure how to get this going. Any help would be greatly appreciated!

You’re on the right track. I would recommend using store.pushPayload and if you want to sideload related models (questionnaire), go ahead and include them in the serialized JSON.

Unfortunately no luck so far.

I’ve tried just doing a straight pushPayload on my JSON data but no takers. It has the following form out of AMS :

{
  "user": {
     id: 1,
     attribute: 'hello"
  },

  "questionnaires": [
    {
      id: 55,
      attribute: "goodbye"
    }
  ]

}

I’m not sure if the issue has to do with the user key being singular? That’s just the way it likes to pop out from AMS … It loads up some of the data, but it is not fixing any attributes that are underscore_separated and changing them to camelCase .

Expected JSON property names can be adjusted by implementing a serializer, especially keyForAttribute.