【发布时间】:2013-07-20 16:40:21
【问题描述】:
我在主干中有一个简单的应用程序,其中逻辑对我来说不清楚,这是我的第二个应用程序,我不知道逻辑是否良好以及我如何构建它。
目标是当我加载一个页面时,我想检索特定目录的所有文件夹和文件并打印它。
我创建了一个应用程序,其中包含另一个名为文件夹的应用程序。
当我加载页面时,我创建了通用应用程序,在文件夹内搜索并将文件夹和文件检索到具有 ajax 功能的 json 中。
我已经成功了,但我的问题是现在如何填充我的 FolderView、FolderModel 和 Folder Collection?
我认为有必要这样做,但我不知道如何将此 json 传递给 FolderCollection/FolderModel。
这是我的应用视图
define(['jquery' , 'backbone', 'views/folder', 'models/folder', 'collections/folder', 'models/app'],
function($, Backbone, FolderView, FolderModel, FolderCollection, AppModel){
var AppView = Backbone.View.extend({
model: AppModel,
el:$('#folder'),
initialize: function(){
console.log('initialize AppView');
this.retrievFolderFile('uploads');
//prendo le cartelle e le metto nel json
this.folders = new FolderCollection();
this.render();
//this.todos.bind('sync', this.render, this);
//this.todos.fetch();
},
render: function(){
var element = this.$el;
element.html('');
this.folders.each(function(model){
var folderView = new FolderView({model:model});
element.append(folderView.el);
});
},
retrievFolderFile: function(dir){
var here = this;
console.log('retrieveFolderFile');
$.ajax({
type: "POST",
dataType: "json",
data: {"dir": dir},
url: 'function/retrieve_folder.php',
success: function(data) {
console.log(data);
// ------------------ HERE I HAVE RESULT HOW TO PASS TO FOLDER VIEW/ COLLECTION?------
}
});
}
})
return AppView;
});
这是我的文件夹模型
define(['backbone'],function(Backbone){
var FolderModel = Backbone.Model.extend({
defaults:{
name:'Folder',
path:''
},
initialize:function(){
console.log('Initialized Folder model');
}
});
return FolderModel;
});
这是我的文件夹集合
define(['backbone', 'models/folder'], function(Backbone, FolderModel){
var folderCollection = Backbone.Collection.extend({
model: FolderModel,
});
return folderCollection;
});
这是我的文件夹视图
define(['jquery', 'underscore', 'backbone', 'text!templates/folder.html'], function($, _, Backbone, FolderTemplate){
var FolderView = Backbone.View.extend({
el:$('#folder'),
template: _.template(FolderTemplate),
initialize: function(){
console.log('FolderView initialized');
this.render();
},
render: function(){
console.log('FolderView render');
//$(this.el).html(this.template({model: this.options.model}));
}
});
return FolderView;
});
为了检索文件夹和文件,我使用这个文件 PHP,它返回一个有效的 json 到主干
<?php
$dir = $_POST['dir'];
$data = fillArrayWithFileNodes( new DirectoryIterator( '../'.$dir ) );
function fillArrayWithFileNodes( DirectoryIterator $dir )
{
$data = array();
foreach ( $dir as $node )
{
if ( $node->isDir() && !$node->isDot() )
{
//$data[$node->getFilename()] = fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
$arr_to_insert = array();
$arr_to_insert['name'] = $node->getFilename();
$arr_to_insert['type'] = 'folder';
array_push($data, $arr_to_insert);
}
else if ( $node->isFile() )
{
//$data[] = $node->getFilename();
$arr_to_insert = array();
$arr_to_insert['name'] = $node->getFilename();
$arr_to_insert['type'] = 'file';
array_push($data, $arr_to_insert);
}
}
return $data;
}
echo json_encode($data);
?>
如何从 appview 传递返回给我的 json? 在 appview 中进行文件夹和字段搜索是否正确,或者我必须在文件夹模型中进行? 因为当我点击一个文件夹后,我想再次检索它的文件和文件夹。 帮助我理解逻辑以及如何通过它。 我想在 FolderCollection 中调用一个函数,如 parseFolderFile 并将其插入模型中,但如何?
谢谢
【问题讨论】:
标签: json backbone.js require