访问Backbone集合中的模型

时间:2013-10-15 14:30:19

标签: javascript backbone.js

我正在努力访问我的收藏中的模型尽管已经阅读了这个问题的各种解决方案并尝试了它们 - 显然我没有得到的东西 - 请帮助!

我有一个数据来自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

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

我似乎无法弄清楚为什么会出现这种情况虽然我的猜测是我只是缺少一些基本的东西? - 骨干noob我害怕!

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

提前致谢

1 个答案:

答案 0 :(得分: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();            
    }
});

现在检查渲染方法中的集合