0

我正在尝试在 RequireJS 之上实现一个简单的 Backbone 应用程序,但事情并没有解决。具体来说,我不能让 Collection 对其自身进行迭代。下面是我从各自文件中提取的模型/集合/视图。

模型:

define([
    'backbone'
], function(Backbone) {
    'use strict';

    var m_Song = Backbone.Model.extend({
    });

    return m_Song;
});

收藏:

define([
    'backbone',
    '../models/model'
], function(Backbone, m_Song) {
    'use strict';

    var c_Songs = Backbone.Collection.extend({
        model: m_Song,

        url: './path/to/JSON.json',

        initialize: function() {
            this.fetch();
        }
    });

    return new c_Songs();
});

看法:

define([
    'jquery',
    'underscore',
    'backbone',
    '../collections/collection'
], function($, _, Backbone, c_Songs) {
    'use strict';

    var v_Main = Backbone.View.extend({
        initialize: function() {
            this.render();
        },

        render: function() {
            var $_html = (function(){
                var $_res = [];

                console.log(c_Songs);

                c_Songs.each(function($_v, $_k) {
/* =====================> Codes in here won't run <===================== */
                });

                _.each(c_Songs, function($_v, $_k) {
/* =====================> Won't run either <===================== */
                });

                return $_res;
            })();

            return this.$('tbody').append($_html);
        }
    });

我做错什么了吗?作为记录,以下是输出console.log(c_Songs);

─ d
  ├─ _byCid
  ├─ _byId
  ├─ length
  └─ models
     ├─ length
     └─ 0
        ├─ _callbacks
        ├─ _escapedAttributes
        ├─ _pending
        ├─ _previousAttributes
        ├─ _silent
        ├─ attributes
           └─ (My JSON response can be found here!)
        ├─ changed
        ├─ cid
        └─ collection

但我确信我不应该以像这样奇怪的方式访问我的 JSON 数据c_Songs.models[0].attributes;一定有什么问题。

我应该如何正确地让 Collection 对其自身进行迭代?

4

1 回答 1

1

感谢 mu ,在这里找到了完美的解决方案!

于 2012-08-27T07:40:22.607 回答