试图用骨干解析静态json

时间:2013-07-04 17:05:09

标签: json backbone.js underscore.js

我正在尝试输出带有主干/下划线的对象列表,但我在主干中得到一个奇怪的输出。我期待一个解析的数据结构,但你可以看到我回来的东西似乎没有以正确的方式解析。有什么想法吗?

这是我的json feed:

{"1":{"title":"Test Video Collection - blah (placeholder)","image":"imagenotavailable"},"2":{"title":"Camur Exerci Gemino","image":"public:\/\/gallery\/large\/imagefield_ieMdB8.jpg"},"3":{"title":"Abdo Nutus Sed","image":"public:\/\/gallery\/large\/imagefield_LtRKG1.png"},"4":{"title":"Quidne Vereor","image":"public:\/\/images\/video\/[current-date:custom:Y\/m\/d]\/imagefield_j7lGlq.jpg"},"5":{"title":"Ludus Ratis","image":"public:\/\/video-collections\/imagefield_aB3dpQ.png"},"6":{"title":"Appellatio Dolore Huic Vulputate","image":"public:\/\/gallery\/large\/imagefield_LtRKG1.png"}}

这是我的代码:

 var itemModel = Backbone.Model.extend();
            var ItemList = Backbone.Collection.extend({
                model: itemModel,
                url: 'http://l.blahcouk.sandbox:8080/franchise/videocollection/date/'
            });

            var ItemsView = Backbone.View.extend({
                template: _.template($('#bandlist_template').html()),
                render: function (eventName) {
                    _.each(this.model.models, function (items) {
                        console.log(items);
                        var lTitle = items.attributes['title'];
                        var lTemplate = this.template(items.toJSON());
                        $(this.el).append(lTemplate);
                    }, this);
                    return this;
                }
            });
            var lItems = new ItemList;
            var AppView = Backbone.View.extend({
                el: "body",

                render: function () {
                    var lItemsView = new ItemsView({
                        model: lItems
                    });
                    var lHtml = lItemsView.render().el;
                    $('#bands').html(lHtml);
                },

                initialize: function () {
                    var lOptions = {};
                    lOptions.success = this.render;
                    lItems.fetch(lOptions);
                }
            });
            var App = new AppView;

这是输出结构:

 r {cid: "c2", attributes: Object, collection: r, _changing: false, _previousAttributes: Object…}
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
1: Object
2: Object
image: "public://gallery/large/imagefield_ieMdB8.jpg"
title: "Camur Exerci Gemino"
__proto__: Object
3: Object
4: Object
5: Object
6: Object
__proto__: Object
changed: Object
cid: "c2"
collection: r
__proto__: s

1 个答案:

答案 0 :(得分:1)

以下是我认为发生的事情 - 您的JSON是有效的,但不是骨干预期的。 Backbone集合期望对象的数组,而不是具有数字键的对象。所以,你需要做的是改变你的json输出更像......

[
{"title":"Test Video Collection - blah (placeholder)","image":"imagenotavailable"},{"title":"Camur Exerci Gemino","image":"public:\/\/gallery\/large\/imagefield_ieMdB8.jpg"},
....
]

而不是

{
"1": {"title":"Test Video Collection - blah (placeholder)","image":"imagenotavailable"},
"2": {"title":"Camur Exerci Gemino","image":"public:\/\/gallery\/large\/imagefield_ieMdB8.jpg"},
....
}

如果无法改变此输出,可以解决此问题。