0

情况是我有一个看起来像这些的集合视图。

ContactManager.module('Entities', function (Entities, ContactManager, Backbone,
    Marionette, $, _) {
    Entities.Contact = Backbone.Model.extend({

    });

    Entities.configureStorage(Entities.Contact);

    Entities.ContactCollection = Backbone.Collection.extend({

        model: Entities.Contact,
        comparator: "firstName"
    });

    var contacts;

    var initializeContacts = function () {
        contacts = new Entities.ContactCollection([{
            id: 1,
            firstName: 'Alice',
            lastName: 'Arten',
            phoneNumber: '555-0184'
        }, {
            id: 2,
            firstName: 'Bob',
            lastName: 'Brigham',
            phoneNumber: '555-0163'
        }, {
            id: 3,
            firstName: 'Charlie',
            lastName: 'Campbell',
            phoneNumber: '555-0129'
        }]);
    };

    var API = {
        getContactEntities: function () {

            if (contacts === undefined) {
                initializeContacts();
            }
            return contacts;
        }
    };

    ContactManager.reqres.setHandler("contact:entities", function () {
        return API.getContactEntities();
    });
});

目前我正在使用静态值显示联系人集合,但现在我想通过调用将返回 json 的 restful web 服务来填充它。我已经尝试了很多事情,但无法做到。

4

1 回答 1

1

这些方面的东西应该起作用:

Entities.ContactCollection = Backbone.Collection.extend({
    model: Entities.Contact,
    comparator: "firstName",
    // this is the API that returns a JSON list of contacts 
    url: '/contacts'
});

var initializeContacts = function () {
    contacts = new Entities.ContactCollection();
    // ask the collection to update itself from the server.
    contacts.fetch();
};
于 2013-09-06T18:05:50.320 回答