0

尽管阅读了有关此问题的各种解决方案并进行了尝试,但我仍在努力访问我收藏的模型-显然有些东西我没有得到-请帮助!

我有一个集合,其中数据来自 XML 文件,如下所示:

define([
    'underscore', 
    'backbone',
    'localstorage',
    'models/script/ScriptModel'
], function(_, Backbone, LocalStorage, ScriptModel) {

    var ScriptsCollection = Backbone.Collection.extend({ 

        model : ScriptModel,

        url: '/data/script.xml',

        parse: function (data) {
        var parsed = [];
            $(data).find('play').each(function (index) {
                var title = $(this).find('title').text();
                var author = $(this).find('author').text();
                parsed.push({
                    title: title,
                    author: author
                });
            });

            return parsed;
        },

        fetch: function (options) {
            options = options || {};
            options.dataType = "xml";

            return Backbone.Collection.prototype.fetch.call(this, options);
        }
    });

    return ScriptsCollection;
}); 

这一切似乎都正常,然后在我看来,我做了以下事情:

define([
    'jquery', 
    'underscore', 
    'backbone',
    'views/settings/SettingsView',
    'models/script/ScriptModel', 
    'collections/scripts/ScriptsCollection',    
    'text!templates/script/scriptTemplate.html'
],    
    function($, _, Backbone, SettingsView, ScriptModel, ScriptsCollection,  scriptTemplate) {

    var ScriptView = Backbone.View.extend({
        el : $("#container"),

        initialize: function(){
            this.collection = new ScriptsCollection();    
            this.collection.fetch();
        },

        render : function() {            
            this.$el.html(scriptTemplate);

            //add the settings view
            var settingsView = new SettingsView();
            settingsView.render();            
        }
    });

    return ScriptView;
});

现在,如果我控制台日志“this.collection”,我可以看到:

child {length: 0, models: Array[0], _byId: Object, constructor: function, model: function…}
    _byId: Object
    length: 1
    models: Array[1]
    0: child
    _changing: false
    _events: Object
    _pending: false
    _previousAttributes: Object
    attributes: Object
    author: "Eric and Ernie"
    title: "The Play Wot I Wrote"
    __proto__: Object
    changed: Object
    cid: "c3"
    collection: child
    __proto__: Surrogate
    length: 1
    __proto__: Array[0]
    __proto__: Surrogate

在“模型”中,我可以在“属性”中看到我的数据,但如果我记录“this.collection.models”,我会看到一个空数组。我还可以看到模型的长度为 0,所以我对此感到困惑??

我似乎无法弄清楚为什么会这样,尽管我的猜测是我只是在这里遗漏了一些基本的东西?- 我害怕骨干菜鸟!

任何想法............任何人?

提前致谢

4

1 回答 1

0

你可以在你的代码中试试这个: var ScriptView = Backbone.View.extend({ el : $("#container"),

    initialize: function(){
        this.collection = new ScriptsCollection();    
        this.collection.fetch({reset: true});
        var that = this;
        this.collection.bind('reset', that.render, that.collection);
    },

    render : function() {            
        this.$el.html(scriptTemplate);
        console.log(this.collection);
        //add the settings view
        var settingsView = new SettingsView();
        settingsView.render();            
    }
});

现在在 render 方法中检查集合

于 2013-10-15T14:48:12.210 回答