使用动态路径为模型打包对象是不确定的 - 为什么?

时间:2015-01-28 18:56:38

标签: ember.js routes ember-router

与EmberJS一起玩,我现在试图了解路线的一些行为。

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

Here's a JsBin.

目前,只有两种观点:索引(所有可用相册的列表)和个人相册视图。

动态路由的定义如下:

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 打包在一个对象中,以便我以后可以将更多信息打包到其中。)

Lyrically Index Page

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

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)单独应该(并且正在)返回一个对象,那么为什么Ember会窒息?

错误本身是无益的:

     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)

1 个答案:

答案 0 :(得分:0)

当模特是Ember对象时,Ember(显然)喜欢它。好消息是将常规对象转换为Ember对象的实例非常容易 - 你只需Ember.create(yourRegularObject)

因此,请返回以下内容

return Ember.Object.create(Lyrically.ALBUMS.result.findBy('key',params.key));

解决了您的问题。

工作演示here