1

I have a simple app in backbone where the logic isn't clear for me, It's my second app and I don't know if is good the logic and how I have construct it.
The goal is when I load a page I want to retrieve all folders and files of a specific directory and print it.
I have create an app general where I include another app called Folder. When I load the page I create the general app, search inside a folder and retireve folders and files into a json with an ajax function.
I have succesfull do that but my problem is how now populate my FolderView, FolderModel and Folder Collection? Is necessary to do that I think but I don't know how to pass this json to FolderCollection/FOlderModel.

This is my appView

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;
});

This is my folderModel

define(['backbone'],function(Backbone){
    var FolderModel = Backbone.Model.extend({
        defaults:{
            name:'Folder',
            path:''
        },
        initialize:function(){
            console.log('Initialized Folder model');
        }
    });

    return FolderModel;
});

This is my FolderCollection

define(['backbone', 'models/folder'], function(Backbone, FolderModel){

    var folderCollection = Backbone.Collection.extend({
        model: FolderModel,
    });

    return folderCollection;
});

This is my FolderView

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;
});

To retrieve folder and files I use this file PHP that return me a valid json to backbone

<?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);
?>

How to pass from appview the json that return me? Is correct to do the folderand filde search inside appview or I have to do it inside folderModel? Because after when I click on a folder I want to retrieve its file and folders again. Help me to understand the logic and how to pass it. I thought to call a function inside FolderCollection like parseFolderFile and insert it into a model but how?

Thanks

4

1 回答 1

1

AJAX异步的 .. 所以你正在进行一个 ajax 调用,然后调用 render 方法,该方法在集合上迭代并呈现它们。

但是在成功回调之前和之后,您的数据还没有准备好,我猜您的集合将被填充。所以你应该在成功回调中填充集合,然后调用渲染。

所以这些行中的一些东西

initialize: function () {
    console.log('initialize AppView');
    this.folders = new FolderCollection();
    this.retrievFolderFile('uploads');
},
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.folders.set(data);
            here.render();
        }
    });
}

另外,您为什么要处理 ajax 请求,因为骨干网已经提供了本机请求。

您可以url在集合中声明 do this.folders.fetch(),然后收听sync或重新发送事件,这在我看来会更干净。

使用本机 Fetch

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 () {
            this.folders = new FolderCollection();
            // This will automatically call the render once the 
            // collection is reset 
            this.listenTo(this.folders, 'reset', this.render);
            this.folders.data = 'uploads';
            this.folders.fetch();
        },
        render: function () {
            var element = this.$el;
            element.html('');
            this.folders.each(function (model) {
                var folderView = new FolderView({
                    model: model
                });

                element.append(folderView.el);
            });
        }
    })

    return AppView;
});

define(['backbone', 'models/folder'], function(Backbone, FolderModel){

    var folderCollection = Backbone.Collection.extend({
        model: FolderModel,
        url : 'function/retrieve_folder.php' + this.data;
    });

    return folderCollection;
});
于 2013-07-20T18:31:32.047 回答