Arrays js如何创建我可以索引的数组
我在索引数组时遇到问题。环顾四周,我发现了一些如何通过视图为控制器中的项目编制索引的示例 我的应用程序从REST调用获取数据,如下所示Arrays js如何创建我可以索引的数组,arrays,ember.js,controllers,indices,Arrays,Ember.js,Controllers,Indices,我在索引数组时遇到问题。环顾四周,我发现了一些如何通过视图为控制器中的项目编制索引的示例 我的应用程序从REST调用获取数据,如下所示 Videos.Store = DS.Store.extend({ revision: 12, url: "http://localhost/", adapter: Videos.Adapter }) Videos.Router.map(function(){ this.resource('videos', {path: '/'}); this.route('f
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();
}
});
我的路由器如下
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}}
我的数据显示
但是,我想从链接实现这个示例
但是我在生成Videos.videoscocontroller并将其传递给{{{each iterator}时遇到了一个问题
我的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。你通过了视频。视频控制器。据我所知,Ember提供了一个要迭代的默认数组集合,但现在我希望扩展这个控制器,我很难创建一个要传递给视图的数据数组。我还不清楚视图代码是如何连接到这个扩展控制器的
任何有助于澄清这一点的想法都将不胜感激
然而,我想从链接实现这个示例,但是我在生成Videos.videoscocontroller并将其传递给{{{each iterator}时遇到了一个问题
虽然这不是推荐的方法,但要实现您试图使用VideosController
执行的操作,您应该创建一个实例,而不是扩展。这看起来像这样:
Videos.videosController = Ember.ArrayController.create({
content: [{ name: 'Goodfellas' }, { name: 'The Aviator' }, { name: 'The Departed' }]
});
然后您可以像这样使用它来迭代:
{{#each Videos.videosController}}
...
{{/each}}
这里需要注意的两件事是,我们不是在扩展ArrayController
,而是直接创建它的一个实例,我们还使用小写的videosController
指出它是一个实例
还有一件值得一提的事情是控制器
不像路线
那样有模型
挂钩
希望能有帮助
编辑
在你上次发表评论后,我意识到你真正想要做的事情,因此我整理了一个小jsbin,展示如何使用模型中的数据为视频提供素材。videosController的内容,请看一看
但基本上,您需要做的是连接到路线的afterModel
挂钩,并将已检索到的模型传递给视频。videosController
的内容:
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);
}
});
这是一个很大的帮助。但我还是被卡住了。我真正需要解决的问题之一是,当路由从模型创建阵列时,我如何使用它?因此,在上面的示例中,您有硬编码的内容:[{name:'Goodfellas'},{name:'foobar'}],但我希望能够像Videos.Video.find()。@LindaKeating,我已经编辑了我的答案,我认为现在它反映了更多您的用例,希望它有所帮助。您好,非常感谢。这对我来说有点意义了。不过我还是被卡住了。我在API中搜索了关于“afterModel”的文档,但没有找到任何文档。代码运行时也不会产生错误,但我认为videosController中的数组中没有填充数据。当我在某个地方调试它时,我得到了一个预期的hash或Mixin实例的错误,get[object]ember。可能不得不放弃!但是谢谢again@LindaKeating是的,新增加的路由器并没有很好的文档记录,但是您可以在这里找到更多关于前模式
,后模式
等的信息。