Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Created August 9, 2012 10:54
Show Gist options
  • Save rlivsey/3303245 to your computer and use it in GitHub Desktop.
Save rlivsey/3303245 to your computer and use it in GitHub Desktop.
Dynamic navigation with Ember.js
App.NavSectionMixin = Ember.Mixin.create({
enter: (router, context) ->
router.set("sectionNavigationController.current", @get("section"))
@_super(router, context)
exit: (router, context) ->
router.set("sectionNavigationController.current", null)
@_super(router, context)
})
...
dashboard: Em.Route.extend(App.NavSectionMixin, {
section: "dashboard"
route: "/"
})
...
App.SectionNavigationView = Ember.CollectionView.extend({
classNames: ["main"]
tagName: "ul"
itemViewClass: Ember.View.extend({
template: Ember.Handlebars.compile("""
<a {{bindAttr href="view.href"}}><span>{{view.content.name}}</span></a>
""")
classNameBindings: ["isCurrent", "sectionClassName"]
sectionClassName: (()->
"nav-section-" + @get("content.section")
).property()
isCurrent: (()->
@get("controller.current") == @get("content.section")
).property("controller.current")
href: (()->
@get("controller.target").urlForEvent(@get("content.action"))
).property()
click: (e) ->
@get("controller.target").send(@get("content.action"), e)
e.preventDefault()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment