Add data to object - and use them in another route

Hi guys,

I’ve got a problem - I know what I want to do, but I have no idea how to start.

I’ve got some html inputs in one of my routes, I want to take data from those inputs. Then I want to put those data in the existing model (or some object - I don’t get all names here in ember yet) and then those data show into another route.

Is there any good soul who will tell newbie how to do this?

You’ve got some options, kinda depends on what the data is and what your routes look like. I’ll try and define some of the terms here so you hopefully have a rough idea of what they’re all for and maybe it will be easier to make a decision.

Route Model - the data that a route loads in its model hook to display or pass to components. Typically this is data from an API or (if you’re making a “create” route, a brand new record that hasn’t been saved to the API but will be.

Ember Data Model - this is what defines the shape of your ember data records, usually mirroring the data that comes from your API. I clarify the two uses of model here because it’s kind of an overloaded term in Ember. Typically you would load Ember Data records into the Ember Data store and they represent “persistent” data from your backend. So this is data that is typically stored on your server and loaded/created/modified in your front-end app.

Service - this is the mechanism for sharing “global” application state in an Ember App. Services are great and may be what you’re looking for here (if not a store record). A service is essentially just an object you can inject into other objects (like components, controllers, routes, etc). Services can have methods, properties, computed properties, etc. just like any other Ember Object. There are many uses for services, from authentication (any part of your app can check authentication state), to the Ember Data store (it’s a service! so you can inject it anywhere), to localization (set the locale anywhere in your app and inject the locale state and data wherever you want), etc. Basically, anytime you want to share data and it’s either NOT data that you want to persist to your backend or NOT data that you can pass directly from a route/controller down to components, a service is one of the first things that should come to mind.

Parent Route/Controller - sometimes if you want to share data across routes, but don’t necessarily want it to be global, it makes sense to keep some of that state in a parent route/controller. And technically the application route/controller is the topmost parent route in any application so that’s another option. That’s what we used to do before services, but a bunch of stuff at the application controller level. Not really recommended though.

Hopefully that helps clarify things a wee bit. If not feel free to ask some follow-up questions!

3 Likes