Skip to content

Instantly share code, notes, and snippets.

@ksol
Last active August 29, 2015 14:03
Show Gist options
  • Save ksol/1b47cb87fcc3300cbacb to your computer and use it in GitHub Desktop.
Save ksol/1b47cb87fcc3300cbacb to your computer and use it in GitHub Desktop.
Example of Ember.PromiseMixin usage
Application.IndexRoute = Ember.Route.extend
sections:
label1: url1
label2: url2
label3: url3
labels: (->
Ember.keys @get('sections')
).property('sections')
model: ->
# Keeping track of all of our promises
object = {}
for label, url of @get('sections')
object[label] = @get('store').findAllByUrl('modelType', url)
object
setupController: (controller, model) ->
@_super(controller, model)
controller.set('labels', @get('labels'))
# Filling our controllers with some accessors
controller.get(label, promise) for label, promise of model
{{#each labels}}
{{!-- This will instance one RowController per section --}}
{{render "row" this}}
{{/each}}
Application.RowController = Ember.Controller.extend Ember.PromiseProxyMixin,
needs: ["index"]
init: ->
@_super()
# PromiseMixin requires the "promise" key to be set
# and fills the "content" once it is resolved, so we have to set up
# the right key/values here
# The template fills "content" with the section name
@set 'section', @get('content')
# We can grab the promise from the index controller via the section name
promise = @get("controllers.index.#{@get('section')}")
@set 'promise', promise if promise
{{#if isPending}}
<p>Loading...</p>
{{else}}
{{!-- Render your stuff here --}}
{{/if}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment