I'm using the new Router
without ember-data so I have to implement my own serialize
/deserialize
for dynamic url segments.
I have an ArrayController
which is populated with data within Application#ready
.
For simplicities sake, lets say I have 2 routes: item > '/item/:item_id'
& items > '/items'
.
The deserialize method in the item
route looks something along the lines of this
deserialze: function(router, params){
return router.get('myController')
.findProperty('id', params['item_id']);
}
If I initialize my app on the items
route and navigate to the item
route for a specific item via an {{action}}
everything works fine. Also, after this point I can manually change the url to specific id
's without a problem.
However, if I try to start my app in the item
route (ie /item/2
), deserialize
returns undefined. I'm assuming this is because Application#ready
hasn't finished populating the controller.
How do I get around this?
Edit: I think this answers my question. Use ember-data.
ASYNCHRONY
One final point: you might be asking yourself how this system can work if the app has not yet loaded Post 1 > by the time
App.Post.find(1)
is called.The reason this works is that
ember-data
always returns an object immediately, even if it needs to kick off a query. That object starts off with an emptydata
hash. When the server returns the data, ember-data updates the object'sdata
, which also triggers bindings on all defined attributes (properties defined usingDS.attr
).
I don't know how to correct your initialization problem, but you definitely don't have to use ember-data
if you don't want to.
Personnaly, I have :
deserialize
method for all Ember.Route
,App.ModelFetcher
class that has class methods :find(id, doneRequestCallback, failRequestCallback)
all(doneRequestCallback, failRequestCallback)
App.ModelFetcher.find(id, function() {}, function() {})
directly in the deserialize
These two methods, as ember-data
does, returns respectively an empty App.Model
object, and an empty array. When the request is done, these objects are inflated with request values.
There could be better solution, but it works fine for me, and does not look so bad.
You could also, instead of overriding the deserialize
method, just define the classic App.Model.find(id)
function (which returns an empty App.Model
instance), and then set a state to your object, like ember-data
does (see https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/states.js).
The method will automatically be called by ember if the dynamic segment has the format model_id
(see https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/routable.js#L251).
Anyway, I could be wrong, but I don't like the fact you're trying to get the object in your controller, but I have no other solution than those describe above.