【问题标题】:Accessing Models in Backbone Collection访问主干集合中的模型
【发布时间】:2013-10-15 14:30:19
【问题描述】:

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

我有一个数据来自 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,所以我对此感到困惑??

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

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

提前致谢

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    你可以在你的代码中试试这个: 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 方法中检查集合

    【讨论】:

    • 在主干 1.0 中更改了获取处理程序。参考链接:backbonejs.org/#changelog
    • 您好 SR5,谢谢。不幸的是,当我按照建议登录渲染方法时,我得到了同样的结果。我现在还收到由“this.collection.bind('reset', this.render) 行引起的“Uncaught TypeError: Cannot call method 'html' of undefined' 错误;'。我想我在某处读到,在旧版本的 Backbone 中通过 fetch 调用了 reset - 我使用的是 1.1.0 - 这可能是我想知道的问题的一部分吗?
    • 更新了答案。请检查。做了同样的改变,我在 render 方法中得到了集合。
    • 在集合获取数据之前调用了 Catch。
    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 2015-05-30
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    相关资源
    最近更新 更多