I just want to be able to display a campaign model's properties in the templates rendered in the my resource template's outlet.
My router looks like this:
App.Router.map (match)->
#resource 'campaigns', ->
#resource 'campaign',
path: ':campaign_id', ->
#route 'chat',
path: '/chat'
I set up 3 templates for my campaign resource:
campaign
campaign.index
campaign.chat
I am not sure how I am supposed to get acces to the model identified by the dynamic segment in my url. I was able to set it up successfully in my campaign route like so:
App.CampaignRoute = Ember.Route.extend
model: (params) -> App.Campaign.find(params.campaign_id)
This doesn't seem to work for the index and chat routes.
I was thinking something like {{someProperty}} or {{campaign.someProperty}} would work by default here.
Why does the context change and how do I get it?
My routes and templates are rendering fine, minus the context I want.
Setting the model this way in my campaign.index and campaign.chat routes made all the difference:
App.CampaignIndexRoute = Ember.Route.extend
model: ->
#modelFor('campaign')
App.CampaignChatRoute = Ember.Route.extend
model: ->
#modelFor('campaign')
Related
I have 2 models in Ember Data: a Tag has and belongs to many Files, both models being loaded via JSON from a Rails backend. My URLs are not deep nested because of the many-to-many nature of the relationship. Here is the code to set that up in Ember:
App.Router.map ->
#resource 'tags', ->
#resource 'tag',
path: 'tags/:tag_id'
, ->
#resource 'file',
path: 'files/:file_id'
, ->
App.Tag = DS.Model.extend
name: DS.attr('string')
files: DS.hasMany('file',
async: true
)
App.File = DS.Model.extend
name: DS.attr('string')
tags: DS.hasMany('tag',
async: true
)
In the tags page I display a list of all tags - no issues here. In the tags/:tag_id page I display the list of files having that tag - also no issues. In the files/:file_id page I want to display some information about the file, with the ability to do things such as edit the tags belonging to that file. Doing so requires me to have the list of all tags, both belonging and not belonging to that file, loaded in Ember, but since the routes are not in the same hierarchy chain, I can't for example do
App.FileController = Ember.ObjectController.extend
needs: 'tags'
tags: Ember.computed.alias('controllers.tags.content') # undefined
to obtain the data I need, although if this were possible, it would be exactly what I need. It also doesn't make sense to pass in all the JSON for all the tags (especially the ones not belonging to the file) in that route.
What are my options at this point? I'm guessing I have to restructure the code to make the tags information accessible in the ApplicationController and calling needs: 'application'. I haven't quite figured out how to do load the correct JSON in the ApplicationController. Is there an alternate/better way that I am missing?
I would set the content of the tags controller in your FileRoute.
App.FileRoute = Ember.Route.extend(
init: ()->
#_super()
#generateController('tags')
setupController : (controller, model) ->
#_super(controller, model)
#controllerFor('tags').set('model', #get('store').find('tag'))
)
That way, you can keep all your other code but I think you should use Ember.computed.alias('controllers.tags.model') instead of Ember.computed.alias('controllers.tags.content').
I'm building an office reception app in Ember. When a person arrives at the office, they pop open the app and are taken through a three step wizard:
Choose a reason for visiting
Choose the person you've come to see
Confirm
The app also allows an administrator to view all of the visits and to view an individual visit.
I have Visit and Person models I've hooked up to a server using Ember Data. Here's what my routes look like:
App.Router.map () ->
#resource 'visits', ->
#resource 'visit', { path: '/:visit_id' }
#resource 'new', ->
#route 'welcome'
#route 'directory'
#route 'thanks'
This allows me to create a new Visit model in the VisitsNewRoute and use it in the welcome, directory and thanks views.
This works. However, it feels wrong to have a new resource, especially since it's conceivable I'll want at least one more new route in my application.
Is there a better way to do this?
I think that you can change the new resource to newVisit like this:
App.Router.map () ->
#resource 'visits', ->
#resource 'visit', { path: '/:visit_id' }
#resource 'newVisit', ->
#route 'welcome'
#route 'directory'
#route 'thanks'
Now you will have a NewVisitRoute where you can create a new Visit model to use in each of the child routes.
And you will be able to make a transition to this routes with the route names: newVisit.welcome, newVisit.directory and newVisit.thanks. You can use this route names in a link-to helper link this:
{{link-to "Welcome", "newVisit.welcome"}}
The recommended practice is to use a create/new route under the resource type, so new under visits, then `transitionTo('visit.welcome', newRecord). (I'm saying all of this with the assumption that welcome, directory, and thanks aren't part of the new record creation).
App.Router.map () ->
#resource 'visits', ->
#route 'new'
#resource 'visit', { path: '/:visit_id' }
#route 'welcome'
#route 'directory'
#route 'thanks'
Ember doesn't always name routes the way you want when dealing with routes nested more than one level. I would name your 'new' route as follows:
#resource 'visits.new', path: 'new', ->
There are a number of approaches you can use to structuring your routes depending on how you assign model ids and whether or not you are using localStorage to preserve user edits until they are persisted to the server.
I have my route pattern as follows:
App.Router.map () ->
#resource 'visits', ->
#route 'new'
#route 'crud', path: ':visit_id'
My 'new' routes create a new resource in the routes model callback which in my models auto-generates a v4 UUID. The new route then performs a transitionTo the crud route in the afterModel callback. In effect the 'visits.new' route acts as a trampoline and allows you to easily use {{link-to 'visits.new'}} from templates/menus etc.
The above approach allows you to to have a single crud route and crud controller that can handle all the show/create/update/delete actions for the model. The models isNew property can be used within your templates to handle any differences between create and update.
I also use localStorage so that newly created (but not yet persisted) models survive a browser refresh, the UUIDs really come in handy for both this and for persisting complex model graphs.
The above router pattern occurs quite a lot in my app so I have defined some base Route classes and a route class builder but the general pattern is as follows:
If using UUIDs:
App.VisitsNewRoute = Em.Route.extend
model: (params, transition)->
App.Visit.create(params)
afterModel: (model,transition) ->
#transitionTo 'visits.crud', model
App.VisitsCrudRoute = Em.Route.extend
model: (params,transition) ->
App.Visit.find(params['visit_id'])
If not using UUID's then the routes are different. I did something like this before I moved to UUIDs, it treats model id 'new' as a special case:
App.Router.map () ->
#resource 'visits', ->
#route 'crud', path: ':visit_id'
App.VisitsCrudRoute = App.Route.extend
model: (params, transition) ->
visit_id = params['visit_id']
if visit_id == 'new' then App.Visit.create() else App.Visit.find(visit_id)
serialize: (model, params) ->
return if params.length < 1 or !model
segment = {}
segment[params[0]] = if model.isNew() then 'new' else model.get('id')
segment
For your specific case of managing the wizard step state I would consider using Ember Query Params, which allow you specify the current step in a parameter at the controller level
Query params example:
App.VisitsCrudController = Em.ObjectController.extend
queryParams: ['step'],
step: 'welcome'
Link to next step in the view:
{{#link-to 'visits.crud' (query-params step="directory")}}Next{{/link-to}}
You may also want to define some computed properties for the next and previous steps, and some boolean properties such as isWelcome, isDirectory for your view logic.
I have a nested edit route:
#resource 'dashboard.communities.community', path: ':community_id', ->
#route 'edit'
In my route, I try to retrieve the model with modelFor:
CivicSourcing.DashboardCommunitiesCommunityEditRoute = Ember.Route.extend
model: (params, queryParams, transition) ->
#modelFor('community')
But this returns undefined. The parent route is successfully retrieving the community, though. Any idea what might be going on?
You're resource name is dashboard.communities.community not community
#modelFor('dashboard.communities.community')
Here's a similar example for colors.cool
http://emberjs.jsbin.com/OxIDiVU/442/edit
How do I dynamically get a route's parent route's model. Say my router looks like this:
App.Router.map ->
#resource 'farms', ->
#resource 'farm', path: '/:farm_id', ->
#resource 'attachments'
#resource 'fields', ->
#resource 'field', path: '/:field_id', ->
#resource 'attachments'
If I have 2 routes: App.FieldRoute and App.AttachmentsIndexRoute, and I want to get the Field model from App.AttachmentsIndexRoute, I could do:
model: ->
#modelFor('field')
How do I do the same thing dynamically, not specifying the model's route explicitly? In other words, if I'm looking at attachments for a Farm instead, the same code should work.
(Isn't there a way in Ember to grab the parent route from a nested route?)
UPDATE:
Doesn't look like there's a clean (API) way to do this. I found some hacks using internal properties like router.currentHandlerInfos. Might have to go that route until Ember team adds a proper way of doing this.
There is no API for that, and I think it will stay that way.
A clean workaround would be to reopen the Route and add this functionality.
See here: Getting parent route in EmberJS
I'm using Ember 1.0 and Ember-Data 1.0Beta. I'm trying to pass a dynamic segment to a route like so:
#resource 'organization', path: 'organizations/:organization_id', ->
#route 'edit'
Then in my edit route:
Whistlr.OrganizationEditRoute = Ember.Route.extend
model: (params) ->
#store.find('organization', params.organization_id)
Unfortunately, the params hash turns up empty. When I inspect it in the console, it's just a simple {}. In turn, params.organization_id is null. This happens even when the URL looks correct: "/organizations/1/edit`
This closely resembles the setup in the Ember guides. What could I be getting wrong?
The reason for this is the dynamic segment (:organization_id) is part of the organization resource and not the edit resource. This means that only OrganizationRoute will have access to the params.organization_id.
However, if you need the model in your OrganizationEditRoute you can use modelFor to access it.
Whistlr.OrganizationEditRoute = Ember.Route.extend
model: (params) ->
#modelFor('organization')
This lets the OrganizationRoute load the model from it's dynamic segment and then the OrganizationEditRoute can just simply access that model like so.