Passing data that doesn't fit into a model to a route


#1

I need some architectural advice.

I am using the ember-cli-pickadate add-on in my application. It works well. As it can be initialized with an options object, I want to pass a JSON object from my Rails app to my Ember app. Here is an example.

{ “calendar”: { “collection_date_options”: { “min”: “2016-04-05T07:00:00.000+02:00”, “max”: “2016-07-04T20:21:26.138+01:00” } “delivery_date_options”: { “min”: “2016-04-05T07:00:00.000+02:00”, “max”: “2016-07-04T20:21:26.138+01:00” } } }

Should I wrestle this into an Ember model somehow, with a custom serializer, or is there a simpler way? Ideally I would like it as a model in my route, because the data needs to be there before the component is rendered.

Any thoughts appreciated!


#2

Depending on what you are after you can just do an Ajax query in your route’s model hook and use that in your template. Ember Data is not required …


#3

Thanks acorn! Yeah that makes senxe I think I have it in there now, will have to play with it this evening. Cheers!


#4

If the options object shouldn’t change throughout the lifetime of the application, you might also consider loading the data in an initializer at application startup and then injecting the object in to the appropriate route/controller/component.


#5

Thanks Blacktiger. I added them a $.getJSON request in my setupController in the end, and split up the returned object there, so I could pass the values into my four components on load. Worked like a charm.

Cheers guys


#6

One thing to note. If you do Ajax calls outside of the model hooks you lose the ability to have Ember handle promise errors for you. So if possible it’s advisable to do Ajax within the hooks …