Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 将目录内容提取到主干中的json_Html_Backbone.js_Render_Models_Fetch - Fatal编程技术网

Html 将目录内容提取到主干中的json

Html 将目录内容提取到主干中的json,html,backbone.js,render,models,fetch,Html,Backbone.js,Render,Models,Fetch,我有一个包含图像的文件夹;我在文件夹uploads/上调用fetch,我的GET返回以下HTML响应(无json等) 但是没有任何内容被呈现或附加到我的左列:那么-->我可以从包含这样的图像的目录中提取吗?还有,如何处理HTML响应,如何将其加载到模型中,使其呈现等(如果有任何方法)?您应该将其绑定到sync事件 此外,我更喜欢使用listenTo this.listenTo(地址'sync',this.render)您应该将其绑定到sync事件 此外,我更喜欢使用listenTo this.l

我有一个包含图像的文件夹;我在文件夹uploads/上调用fetch,我的GET返回以下HTML响应(无json等)


但是没有任何内容被呈现或附加到我的左列:那么-->我可以从包含这样的图像的目录中提取吗?还有,如何处理HTML响应,如何将其加载到模型中,使其呈现等(如果有任何方法)?

您应该将其绑定到
sync
事件

此外,我更喜欢使用
listenTo


this.listenTo(地址'sync',this.render)
您应该将其绑定到
sync
事件

此外,我更喜欢使用
listenTo


this.listenTo(地址,'sync',this.render)
您应该将HTML响应更改为JSON格式,以便
Backbone
可以正确地呈现它(尽管有一种方法可以显示上面的
HTML
,但这不是推荐的方法,因为最好呈现原始数据)

您可以这样做:

HTML:

<div id="container">
</div> 
<script id="template" type="text/html">
    <li><img src=<%- dir %><%- image %> /></li>
</script>
$(function(){
    /** Your response object would look something like this. */
    var json = {'parent_directory': 
                   {'dir_desc': 'Index of /backbone_images/uploads',
        'images': [
            {'dir': '/images/', 'image': 'image1.jpg'}, 
            {'dir': '/images/', 'image': 'image2.jpg'}, 
            {'dir': '/images/', 'image': 'image3.jpg'}
        ]
    }};

    /** Create a simple Backbone app. */
    var Model = Backbone.Model.extend({});

    var Collection = Backbone.Collection.extend({
        model: Model
    });

    var View = Backbone.View.extend({
        tagName: 'ul',
        initialize: function() {
            this.render();
        },
        template: _.template($('#template').html()),
        render: function() {
            _.each(this.collection.toJSON(), function(val){ 
                this.$el.append(this.template({
                    image: val.image, 
                    dir: val.dir}));
            }, this);
            return this;
        }
    });

    /** Create a new collection and view instance. */
    var newColl = new Collection(json.parent_directory.images);
    var newView = new View({collection: newColl});
    $('#container').html(newView.el);
});

您应该将HTML响应更改为JSON格式,以便
Backbone
可以正确地呈现它(尽管有一种方法可以显示上面的
HTML
,但这不是推荐的方法,因为最好呈现原始数据)

您可以这样做:

HTML:

<div id="container">
</div> 
<script id="template" type="text/html">
    <li><img src=<%- dir %><%- image %> /></li>
</script>
$(function(){
    /** Your response object would look something like this. */
    var json = {'parent_directory': 
                   {'dir_desc': 'Index of /backbone_images/uploads',
        'images': [
            {'dir': '/images/', 'image': 'image1.jpg'}, 
            {'dir': '/images/', 'image': 'image2.jpg'}, 
            {'dir': '/images/', 'image': 'image3.jpg'}
        ]
    }};

    /** Create a simple Backbone app. */
    var Model = Backbone.Model.extend({});

    var Collection = Backbone.Collection.extend({
        model: Model
    });

    var View = Backbone.View.extend({
        tagName: 'ul',
        initialize: function() {
            this.render();
        },
        template: _.template($('#template').html()),
        render: function() {
            _.each(this.collection.toJSON(), function(val){ 
                this.$el.append(this.template({
                    image: val.image, 
                    dir: val.dir}));
            }, this);
            return this;
        }
    });

    /** Create a new collection and view instance. */
    var newColl = new Collection(json.parent_directory.images);
    var newView = new View({collection: newColl});
    $('#container').html(newView.el);
});

我试图在我的初始化函数中添加这个.listenTo(地址'sync',this.render),但我仍然遇到同样的问题,没有从提取中加载任何模型,也没有向正文添加任何内容我试图在我的初始化函数中添加这个.listenTo(地址'sync',this.render),我仍然遇到同样的问题,没有从fetch加载任何模型,也没有向body添加任何内容您正在获取HTML?@steveax我显然没有做正确的事情,但我基本上是尝试获取目录的内容,它返回的是上面的HTML你正在获取HTML?@steveax我显然没有做正确的事情,但我基本上是在尝试获取目录的内容,它返回的是上面的HTML这几乎可以正常工作,但我的网页浏览器显示的是我的网页,因此var json的设置有点问题,等等,还有一个问题,如果我从目录中提取并接收HTML,我能用同样的方法将其更改为JSON吗?您需要将服务器更改为发回
JSON
,而不是
HTML
。确保您的
JSON
对象包含一个属性,该属性的值为
Objects数组
,这样您就可以对它们进行循环。如果需要呈现这些值,还需要调用
toJSON()
。这几乎可以正常工作,但我的网页浏览器中显示了我的页面,因此var json的设置方式有点问题,等等,还有一个问题,如果我从目录中提取,接收HTML时,我能用同样的方法将其转换为JSON吗?您需要将服务器更改为发回
JSON
,而不是
HTML
。确保您的
JSON
对象包含一个属性,该属性的值为
Objects数组
,这样您就可以对它们进行循环。如果需要呈现这些值,还需要调用
toJSON()