samedi 4 avril 2015

Ember Routing with renderTemplate and setupController

I am building an Ember app to show a simple Twitter-like tagging system. When a user visits /items, he or she will see a list of all items. When the user visits /tags, the user will see a list of tags as links. When the user clicks one of these links, the user should be directed to /tags/:id and will see all items tagged with that specific tag. Then the user will be able to search/sort/manipulate the items as he/she would be able to from the ItemsRoute.


How can I make TagRoute use ItemsController and render the items template, using the tag's associated items as the model?


I have tried different combinations of the hooks in TagRoute, and I'm not able to find a recipe that works. There seems to be a fundamental misunderstanding on my part.


Here is my relevant code:


router.js.coffee



App.Router.map ()->
@resource 'items'
@resource 'tags', ->
@resource 'tag', path: ':tag_id'


routes/tag.js.coffee



App.TagRoute = Ember.Route.extend
model: (params)->
@get('store').find 'tag', params.tag_id
controllerName: 'items'
setupController: (controller, model)->
@controllerFor('items').set('model', model.items)
renderTemplate: ->
@render 'items', ->
into: 'tags'
controller: 'items'


templates/tags.hbs



<ul class="tag-list">
{{#each tag in model}}
<li>
{{#link-to 'tag' tag}}
{{tag.name}}
{{/link-to}}
</li>
{{/each}}
</ul>

{{outlet}}


models/items.js.coffee



App.Item = DS.Model.extend(
body: DS.attr('string')
checked: DS.attr('boolean')
tags: DS.hasMany('tag')
)


models/tags.js.coffee



App.Tag = DS.Model.extend(
name: DS.attr('string')
taggings_count: DS.attr('number')
items: DS.hasMany('item')
)


Currently, this give me an error:



Error while processing route: tag Cannot assign to read only property 'name' of function () {
return {
into: 'tags',
controller: 'items'
};
} TypeError: Cannot assign to read only property 'name' of function () {
return {
into: 'tags',
controller: 'items'
};
}


Looking at the Ember Routes Inspector in Chrome, the controllerName property is the only one which overrides Ember's defaults, and Ember still tries to render a generated tag template.





Aucun commentaire:

Enregistrer un commentaire