我正在嘗試顯示驅動器,其文件夾和文件(潛水hasMany文件夾hasMany文件)的基本應用程序...當使用車把模板實現所有關係加載罰款,但是當我創建控制器和視圖手動我遇到了一些問題。我手動實例化視圖的原因如下。一個要求是根據文件模型上的屬性「type」爲文件對象加載不同的視圖(可能還有控制器)(例如,圖像文件應該具有App.ImageFileView,視頻具有App.VideoFileView)。另一個要求是路由只適用於驅動器,因此不應該有文件夾或文件路徑。我面臨的主要問題是與當前驅動器相關的文件夾加載正常。但是當我遍歷一個文件夾來加載相關文件時,我總是得到一個空集,任何幫助表示讚賞。所以我的問題是爲什麼在使用handlebars模板時顯示文件,而在手動實例化控制器和視圖時不顯示這些文件?我如何強制關係被加載?Ember js和多級關係
小提琴位於here
App = Ember.Application.create({});
// routing
App.Router.map(function() {
this.resource('drives',{path:'/'});
this.resource('drive',{path:'/:drive_id'});
});
App.DrivesRoute = Ember.Route.extend({
model: function() {
return App.Drive.find();
}
});
App.DriveRoute = Ember.Route.extend({
model: function(params) {
rval = App.Drive.find(params.drive_id);
App.set('activeDrive',rval);
return rval;
},
setupController : function(controller, model){
debugger;
var rController = App.DrivesController.create({content:model.get('folders')});
rController.populate();
}
});
// controllers
App.DrivesController = Ember.ArrayController.extend({
populate : function(){
var drives = this.content;
debugger;
drives.forEach(function(drive){
var rc = App.DriveController.create({content:drive});
rc.populate();
var rv = App.DriveView.create({controller:rc});
rv.prepare().append('#output');
});
}
})
App.DriveController = Ember.ObjectController.extend({
populate:function(){
console.log('There are '+this.content.get('files.length')+' files');
this.content.get('files').forEach(function(file){
// not reaching this point ... files.length is always 0
});
}
});
// views
App.DriveView = Ember.View.extend({
template : Ember.Handlebars.compile('{{content.name}}'),
prepare:function(){
return this;
}
});
//Models
App.Store = DS.Store.extend({
revision: 11,
adapter: DS.FixtureAdapter.create({simulateRemoteResponse: false})
});
App.Drive = DS.Model.extend({
title : DS.attr('string'),
folders : DS.hasMany('App.Folder')
});
App.Folder = DS.Model.extend({
name : DS.attr('string'),
drive : DS.belongsTo('App.Drive'),
files : DS.hasMany('App.File')
});
App.File = DS.Model.extend({
content : DS.attr('string'),
type : DS.attr('string'),
folder : DS.belongsTo('App.Folder')
});
// Fixtures
App.Drive.FIXTURES = [
{
id:1,
title : 'First Drive Title',
folders : [11,12,13]
},
{
id:2,
title: 'Second Drive Title'
},
{
id:3,
title: 'Third Drive Title'
}
];
App.Folder.FIXTURES = [
{
id:11,
name:"Docs",
files : [111,112]
},
{
id:12,
name:"Downloads"
},
{
id:13,
name:"Music"
},
];
App.File.FIXTURES = [
{
id :111,
content : 'first file content',
type : 'Text',
folder: 11
},
{
id :112,
content : 'second file content',
type : 'Image',
folder:11
},
];
您可能希望在帖子末尾添加一個問題,這將有助於觀衆回答點 – Freakyuser
在段落末尾添加了一行改寫問題。它現在有什麼意義嗎? – samio