Understanding Ember.JS itemController



我正在浏览EmberJS文档,遇到了一个绊脚石:

ITEM CONTROLLER
It is often useful to specify a controller to decorate individual items in the ArrayController while iterating over them. This can be done in the ArrayController definition:
App.SongsController = Ember.ArrayController.extend({
  itemController: 'song'
});

不完全确定itemController在这里的目的是什么。song属性现在是否代理到名为App.SongsControllerArrayControlleritemController究竟在这里做什么?

当Songs数组中的每个元素以某种方式使用(即在{{each}}循环中渲染)时,每个歌曲都会实例化自己的控制器。因此,您可以有一个歌曲列表,每首歌曲都可以有自己的属性(如isPlaying),也可以编写action处理程序来响应用户交互playpause

对于数组中的每首歌曲,您的SongController都将存在。

App.SongController = Ember.ObjectController.extend({
  isPlaying: false,
  actions: {
    someAction: ...
  }
});

如果您不需要这些属性,也不需要在此控制器上处理操作,则可以关闭itemController。

最新更新