asynchronous - Ember: Using async data in controller -
i have asynchronous call in ember controller, @authsession.currentuser.clients. results of request filtered populate drop down menu.
filterclientsby: (term) -> filter = new regexp("^#{term}", 'i') @authsession.currentuser.clients.filter (client) -> filter.test(client.displayname) the problem filter doesn't work first time because doesn't wait clients come server. feel i'm missing fundamental ember here. looks clients modelarray. can treat promise somehow? there way bootstrap data controller before rendering template?
ok, there couple of things here.
because there asynchronous call in there need use promise in order wait clients relationship come back. (this assuming have "async" set on client relationship in model)
this leaves couple of options (i did these in javascript , converted them coffeescript, may bit strange):
1) return promise "filterclientsby" so:
filterclientsby: (term) -> self = filter = new regexp("^" + term, "i") new ember.rsvp.promise((resolve, reject) -> self.authsession.currentuser.clients.then (clients) -> filteredlist = clients.filter((client) -> filter.test client.displayname ) resolve filteredlist return return ) 2) create property on controller called "filteredclients" set inside resolved "clients" promise block above (instead of returning promise).
filterclientsby: (term) -> self = filter = new regexp("^" + term, "i") self.authsession.currentuser.clients.then (clients) -> filteredlist = clients.filter((client) -> filter.test client.displayname ) self.set "filteredclients", filteredlist return return
Comments
Post a Comment