Ember.js 为具有动态路由的模型打包对象未定义—;为什么?

Ember.js 为具有动态路由的模型打包对象未定义—;为什么?,ember.js,routes,ember-router,Ember.js,Routes,Ember Router,和EmberJS一起玩,我现在正试图了解一些路由行为 我正在构建一个测试应用程序,目的是连接到Rdio API并为各种艺术家显示专辑/歌曲 现在,只有两个视图:索引(所有可用相册的列表)和单独的相册视图 动态路线的定义如下: App.Router.map(function() { this.route("album", { path: "/album/:key" }); }); 给定一个像这样的固定装置 App.ALBUMS = { "status": "ok", "result"

和EmberJS一起玩,我现在正试图了解一些路由行为

我正在构建一个测试应用程序,目的是连接到Rdio API并为各种艺术家显示专辑/歌曲

现在,只有两个视图:索引(所有可用相册的列表)和单独的相册视图

动态路线的定义如下:

App.Router.map(function() {
  this.route("album", { path: "/album/:key" });
});
给定一个像这样的固定装置

App.ALBUMS = {
 "status": "ok", 
 "result": [
  {
   "key": "a5337866", 
   "icon": "http://rdio3img-a.akamaihd.net/album/a/0/3/000000000051730a/3/square-200.jpg", 
   "artist": "The Decemberists", 
  }, 
  {
   "key": "a5229271", 
   "icon": "http://rdio1img-a.akamaihd.net/album/7/d/a/00000000004fcad7/1/square-200.jpg", 
   "artist": "Nicki Minaj", 
  }, 
  ]
};
…索引工作正常:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return {albums:App.ALBUMS.result }
  }
});
(我有意将App.ALBUMS.result打包为一个对象,以便以后可以将更多信息打包到其中。)

但是,当我进入相册视图时,我遇到了一个问题:

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
  }
});
将返回值(应该已经是一个对象)打包到第二个对象或数组中,它就可以工作了

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

App.ALBUMS.result.findBy('key',params.key)应该(并且正在)单独返回一个对象,那么为什么余烬会阻塞它呢

错误本身是毫无帮助的:

     Error while processing route: album undefined is not a function TypeError: undefined is not a function
    at EmberObject.extend._setupArrangedContent (http://local.lyrically.com/js/libs/ember-1.9.1.js:34181:27)
    at null._arrangedContentDidChange (http://local.lyrically.com/js/libs/ember-1.9.1.js:34167:14)
    at applyStr (http://local.lyrically.com/js/libs/ember-1.9.1.js:19677:29)
    at sendEvent (http://local.lyrically.com/js/libs/ember-1.9.1.js:14115:13)
    at notifyObservers (http://local.lyrically.com/js/libs/ember-1.9.1.js:17488:9)
    at propertyDidChange (http://local.lyrically.com/js/libs/ember-1.9.1.js:17291:7)
    at iterDeps (http://local.lyrically.com/js/libs/ember-1.9.1.js:17373:11)
    at dependentKeysDidChange (http://local.lyrically.com/js/libs/ember-1.9.1.js:17329:9)
    at propertyDidChange (http://local.lyrically.com/js/libs/ember-1.9.1.js:17287:9)
    at iterDeps (http://local.lyrically.com/js/libs/ember-1.9.1.js:17373:11)
余烬(显然)喜欢模型是余烬对象。好消息是,将常规对象转换为Ember对象的实例非常容易,只需执行
Ember.create(yourRegularObject)

因此,返回以下

返回Ember.Object.create(lyricly.ALBUMS.result.findBy('key',params.key))

修复您的问题

工作演示