【问题标题】:How to call a webservice in marionette.js framework CollectionView如何在 marionette.js 框架 CollectionView 中调用 Web 服务
【发布时间】:2013-09-06 10:05:15
【问题描述】:

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

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 服务来填充它。我已经尝试了很多东西,但无法做到。

【问题讨论】:

    标签: jquery web-services backbone.js marionette restful-url


    【解决方案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();
    };
    

    【讨论】:

    • 感谢您的有用回答 +1。克里斯,你能帮我吗?我无法在视图中显示它。
    猜你喜欢
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2016-07-14
    • 2020-12-07
    相关资源
    最近更新 更多