Best practices for using single route for different resources in Ember?


#1

I need some advice on how to best handle a specific situation with ember.

I am building some kind of goodreads clone. In my backend, there are books, there are users and there are notes that connect the users to the books (a user has many books through the notes).

When a logged in user visits a page like /books/1, he should see a page with information about the book and a button to add it to his own library.

When a logged in user visits the same page, and he has the book in the library, he should not only see the book information, but a button to remove a book from the library and other fields, eg a textarea to add comments. (I suppose this means that a BookController and a NoteController would be activated simultaneously)

What is the best way to do this in Ember? What would the router for /books/:book_id do? How can I instantiate the correct controllers depending on whether the user is signed in, and whether he has the book in the library?

Thank you

UPDATE 13/5

What I tried

The code below shows my current attempt. I dislike it because of the need to branch every time I do something on the page, depending on whether it is a note or a book. I feel like there should be a much better way to do this…

# routes
App.Router.map ()->
@resource('books', ->
	@resource('book', { path: ':book_id' } )
)

App.BookRoute = Em.Route.extend
	model: (params) ->
		book = @store.find('book', params.book_id)
		if @auth.get('signedIn') and book.get('inLibrary')
			return @store.filter('note', { book_id: params.book_id, inLibrary: true }, (note) ->
				note.get('book_id') == params.book_id and note.get('inLibrary')
			)
		else
			return book
# controller
App.BookController = Ember.ObjectController.extend
	
	note: Em.computed( ->
		if @auth.get('signedIn') && @get('inLibrary')
			note = @store.find('note', { book_id: @get('content.id'), inLibrary: true })
		note
	).property('content')

	signedInAndBookInLibrary: Em.computed( ->
		if @auth.get('signedIn') && @get('inLibrary')
			true
		else
			false
	).property('inLibrary')
 # template
{{#if signedInAndBookInLibrary}}
	{{render 'book/_note' note}}
{{else}}
	{{render 'book/_book' this}}
{{/if}}