view page source - page history - about editing

Revision History: Adding properties to a model within an EmberJS router

This is revision 326 of the page Adding properties to a model within an EmberJS router, as it appeared on Tue, 21 Jan 2014 20:47:21 -0800.
EmberJS

With the following router and handlebars code, I wanted to add a runtime property from the router to the template/view (note, using CoffeeScript):

Todos.TodosIndexRoute = Ember.Route.extend(
	model: ->
		@modelFor 'todos'

	renderTemplate: (controller) ->
		@render 'todos/index',
			controller: controller
)

What you need to do is create a new setupController function, but this must both set the model again, along with any other parameters:

Todos.TodosIndexRoute = Ember.Route.extend(
	model: ->
		@modelFor 'todos'

	renderTemplate: (controller) ->
		@render 'todos/index',
			controller: controller

	setupController: (controller, model) ->
		controller.set 'route', 'index'
		controller.set 'model', model     // critical - otherwise 'model' will be lost
)

This can then be used as normal Handlebars code, i.e.:

<script type="text/x-handlebars" data-template-name="todos/index">
  {{{empty}}{route}}
  <ul id="todo-list">
    {{{empty}}{#each itemController="todo"}}
      // ...
    {{{empty}}{/each}}
  </ul>
</script>

See also this StackOverflow discussion (not sure if it's related).

view page source - what links to here? - page history - top
Last edited by jevon jevon 53 months ago