ember.js - Ember router: Asynchronous model (promises?) -
ember.js - Ember router: Asynchronous model (promises?) -
[this new 1.0.0-pre.4+ router.]
i want homecoming ember route's model
method record needs asynchronous callbacks load, instance because requires load multiple (nested) models. what's best way this?
here sample code hypothetical blog app illustrates problem:
app.router.map -> @resource 'filteredarticles', path: '/:filter' app.filteredarticlesroute = ember.route.extend model: (params) -> blog = app.blog.find(1) # user's blog singleton property = switch params.filter when 'published' 'publishedarticles' when 'draft' 'drafts' when 'all' 'articles' # homecoming list of articles `blog` record. # `blog` hasn't finished loading :( blog.get(property)
i'm in middle of rewriting travis ci newest ember version , faced same problem - fetch repositories slug (e.g. emberjs/ember.js
), not primary key. solution involves using ember.proxyobject
.
when enters path /emberjs/ember.js
, params like:
{ owner: 'emberjs', name: 'ember.js` }
and slug equal emberjs/ember.js
.
with such information, create simple ember object, keeps slug
, isloaded
properties:
content = ember.object.create slug: slug, isloaded: false
then create proxy object content:
proxy = ember.objectproxy.create(content: content)
now can load record server using slug , homecoming proxy model. when record server, set proxies content actual record.
full solution here:
deserialize: (params) -> slug = "#{params.owner}/#{params.name}" content = ember.object.create slug: slug, isloaded: false proxy = ember.objectproxy.create(content: content) repos = travis.repo.byslug(slug) observer = -> if repos.get 'isloaded' repos.removeobserver 'isloaded', observer proxy.set 'content', repos.objectat(0) if repos.length proxy.set('content', repos[0]) else repos.addobserver 'isloaded', observer proxy
you can take @ rest of code on github
ember.js ember-router
Comments
Post a Comment