1

我在索引数组时遇到问题。通过环顾四周,我发现了一些如何通过视图http://jsfiddle.net/WSwna/14/在控制器中索引项目的示例

我的应用程序从 REST 调用中获取数据,如下所示

Videos.Store = DS.Store.extend({
revision: 12,
url: "http://localhost/",
adapter: Videos.Adapter
})

我的路由器如下

Videos.Router.map(function(){
this.resource('videos', {path: '/'});
this.route('forms');
})

Videos.VideosRoute = Ember.Route.extend({
    model: function(){
        return Videos.Video.find();
    }
});

当我将 HTML 实现为

{{#each controller}}
.....
{{/each}}

我的数据显示。

但是我想从链接http://jsfiddle.net/WSwna/14/实现示例 但是我在生成 Videos.VideosController 并将其传递给 {{#each 迭代器}} 时遇到问题

我的 Videos.VideosController 看起来像这样

    Videos.VideosController = Ember.ArrayController.extend({
    model: function(){
        return Videos.Video.find();
    }
});


Videos.VideoView = Ember.View.extend({
    content: null,
    adjustedIndex: function(){
        return this.getPath('_parentView.contentIndex') + 1;
    }.property()
});

但是当我在 HTML 中使用它时,如下所示:

{{#each Videos.VideosController }}

我收到一条错误消息,告诉我内容必须实现 Ember.Array。你通过了 Videos.VideosController。据我了解,Ember 提供了一个要迭代的默认数组集合,但现在我希望扩展这个控制器,我很难创建一个数据数组以传递给视图。我也不清楚 View 代码是如何连接到这个扩展控制器的。

任何可以帮助澄清这一点的想法将不胜感激。

4

1 回答 1

2

However I want to implement the example from the link http://jsfiddle.net/WSwna/14/ But I have a problem with generating a Videos.VideosController and passing that to the {{#each iterator}}

Altough it's not the recomended way, but to achieve what you are trying to do with your VideosController you should create an instance rather then extending. This would look something like this:

Videos.videosController = Ember.ArrayController.create({
  content: [{ name: 'Goodfellas' }, { name: 'The Aviator' }, { name: 'The Departed' }]
});

then you can use it like this to iterate over:

{{#each Videos.videosController}}
  ...
{{/each}}

Two things to note here, we are not extending the ArrayController but instead creating directly an instance of it, and we are also using lower case videosController to point out that it's an instance.

One more thing worth mentioning is that a controller does not have a model hook like a route has.

Hope it helps.

EDIT

After your last comment I realized what you really wanted to do, so I've put together a small jsbin showing how you could use the data from your model to feed your Videos.videosController's content, have a look here.

But basically what you have to do is to hook into the afterModel hook of your route and pass the already retrieved model to your Videos.videosController's content:

Videos.videosController = Ember.ArrayController.create({
  content: []
});

Videos.VideosRoute = Ember.Route.extend({
  model: function(){
    return Videos.Video.find();
  },
  afterModel: function(model) {
    Videos.videoController.set('content', model);
  }
});
于 2013-07-27T22:13:19.480 回答