mercredi 28 janvier 2015

Packaging an object for a model with a dynamic route is undefined — why?

Playing around with with EmberJS, and I'm trying to understand some behaviour with routes right now.


I'm building a test app with the intention of hooking into the Rdio API and displaying albums/songs for various artists.


Right now, there are only two views: The Index (a listing of all available albums), and an individual Album view.


The dynamic route is defined like so:



App.Router.map(function() {
this.route("album", { path: "/album/:key" });
});


And given a fixture that looks like this...



App.ALBUMS = {
"status": "ok",
"result": [
{
"key": "a5337866",
"icon": "http://ift.tt/18vcQPe",
"artist": "The Decemberists",
},
{
"key": "a5229271",
"icon": "http://ift.tt/18vcOam",
"artist": "Nicki Minaj",
},
};


...the index works just fine:



App.IndexRoute = Ember.Route.extend({
model: function() {
return {albums:App.ALBUMS.result }
}
});


(I've intentionally packaged App.ALBUMS.result in an object so that I can pack more information into it later.)


Lyrically Index Page


However, when I get to the Album view, I get a problem:



App.AlbumRoute = Ember.Route.extend({
model: function(params){
console.log(params);
console.log(App.ALBUMS.result.findBy('key',params.key)); //Logs object just fine
return App.ALBUMS.result.findBy('key',params.key); //ERROR
}
});


Packaging that return value (which should already be an object) in a second object or array though, and it works.



// THIS WORKS!:
return [App.ALBUMS.result.findBy('key',params.key)];
// THIS AlSO WORKS!:
return {album: App.ALBUMS.result.findBy('key',params.key)};


Why?


The error itself is rather unhelpful:



Error while processing route: album undefined is not a function TypeError: undefined is not a function
at EmberObject.extend._setupArrangedContent (http://ift.tt/18vcOqA)
at null._arrangedContentDidChange (http://ift.tt/1yykQUE)
at applyStr (http://ift.tt/18vcQPi)
at sendEvent (http://ift.tt/1yykRYQ)
at notifyObservers (http://ift.tt/18vcQPo)
at propertyDidChange (http://ift.tt/1yykQUL)
at iterDeps (http://ift.tt/18vcQPq)
at dependentKeysDidChange (http://ift.tt/1yykSfa)
at propertyDidChange (http://ift.tt/18vcOqL)
at iterDeps (http://ift.tt/18vcQPq)




Aucun commentaire:

Enregistrer un commentaire