Loading a model instance with ember data external to an ember app


#1

I am trying to add an ember app to an existing (large) rails app. If it works out, I would like to use ember for many new areas of the app, but I’m starting small.

Essentially, the user is on a rails Students index page, clicks on one of many links to manage a student’s IEPs, and then the modal box containing the ember app is shown. When we first spiked this back in May, we did this as follows:

showDialog: (url) ->
  studentId = ...
  $('#emberDialog').dialog
    open: ->
      student = App.Student.find(studentId)
      App.Router.router.transitionTo('studentIeps.index', student)```

Now that we are picking this up again, I'm finding that in the bump from Ember V1.0.0-rc.5-80-gf54e8ef to V 1.1.3+pre.e0ffbf84 and Ember-data from V0.13-30 to V1.1.0-beta.3-4 the Model.find methods have been removed.

Is there a supported way to do this any longer? I'm trying to build an instance of a model external to the ember app, and then use that to set the ember app's current route. Halp?

#2

Many of the methods that used to exist on a DS.Model have moved to DS.Store. So in order to find in your case, you would call:

@store.find('student', studentId).then (student) ->
  # do stuff

#3

Thanks for the help. The problem is how to access @store. Remember I’m external to the Ember app.


#4

The unofficial way to do that is to look up the store in your application container:

App.__container__.lookup 'store:main'

#5

Well, that’s definitely a step towards a solution, but for this to be production ready I don’t feel comfortable hacking into the internals in that way. Has Ember data completely removed a public api to access store externally?

Or maybe there is a better way to get an instance of a model outside the ember app?


#6

This is by design, you shouldn’t be trying to poke around an Ember app from the outside. The store is able to be accessed internally from controllers, routes, and models by means of dependency injection, so it’s intentionally isolated.


#7

That’s what I needed to know, thanks. Dang if that doesn’t remove a hell of a lot of options on how to stitch ember to an existing app, though.


#8

I also have a Rails app that is sort of a hybrid and I agree that it can be a challenge to figure out how to make those seams work!

In this case it seems like your open method is doing what an Ember Route normally would. It’s finding the model and then going somewhere. I’ve tried to deduce as much as I can from the code snippet that you posted and this is what I think you should try.

  1. Instead of doing any custom click events on the link just set the href to go to a URL like “myapp.com/students/#/students/12”.
  2. Setup the router send ‘/students/12’ to the StudentIepsIndexRoute.
  3. Finally find the student in the StudenIepsIndexRoute.

If all of your entry points to your Ember app are just links with ‘/#/something’ appended then you can hop back and forth between server rendered pages and Ember rendered ones.