mardi 11 avril 2017

How do I set a class on a parent element when a route is the first one loaded?

I have an Ember demo app that works fine if the first route loaded is 'index', 'list' or 'list/index', but not if the first route loaded is 'list/show'. Code is at http://ift.tt/2nGdV1g , demo is running at http://ift.tt/2nluTyx To see the problem, set your window narrower than 640px and surf to http://ift.tt/2o34dD4 You'll see the list panel, rather than the detail panel.

The underlying problem is that, when the route is 'list/show', the divs with class 'panelList' and 'panelDetail' should also have the class 'right'.

I can't set this in the template, because panelList and panelDetail are created by the parent 'list' template. If I move panelList and panelDetail to the child templates 'list/index' and 'list/show', then the list gets re-rendered when going from 'list/index' to 'list/show' which would be a terrible performance hit.

Currently, I use the 'didTransition' action to toggle the class 'right'. This is called both then transitioning from 'list/index' to 'list/show', and when 'list/show' is the initial route. Unfortunately, if 'list/show' is the first route, none of the DOM elements exist when 'didTransition' is called.

I can envision two routes to a solution, but don't know how to implement either:

  1. Toggle the class 'right' on some action which happens after DOM elements exist.

  2. Insert conditional code in the 'list' template, which sets the class 'right' on 'panelList' and 'panelDetail' if the actual route is 'list/show'.

Suggestions?




Aucun commentaire:

Enregistrer un commentaire