backbone marionette如何从api获取数据

时间:2015-03-29 20:05:05

标签: javascript backbone.js marionette

我是BB和木偶的新手,在阅读了很多文件后我试图建立一些简单的东西.. 我有一个带有用户信息的模型,但是我想从服务器获取用户列表,如何获取数据?

  ContactManager.module("Entities", function(Entities, ContactManager, Backbone, Marionette, $, _){
  Entities.Contact = Backbone.Model.extend({
    defaults: {
        firstName: '',
        lastName: '',
        phoneNumber:''
   },
   initialize: function(){
        console.log('This model has been initialized.');
    }
  });

  Entities.ContactCollection = Backbone.Collection.extend({
    model: Entities.Contact,
    url: 'get_all_scan_for_user'
  });

  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" }
    ]);*/
    contacts = new Entities.ContactCollection();
    contacts.fetch;
  };

  var API = {
    getContactEntities: function(){
      if(contacts === undefined){
        initializeContacts();
      }
      return contacts;
    }
  };

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

我的控制器只是调用请求

var contacts = ContactManager.request("contact:entities");

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

  1. contacts.fetch => contacts.fetch()
  2. 可以 url:'get_all_scan_for_user'获取联系人列表吗?
  3. API.getContactEntities()将始终返回相同的联系人列表。 我应该

    getContactEntities: function(){ if(contacts === undefined){ initializeContacts(); } contacts.fetch(); return contacts; }