Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Nesting - General Architecture

I've been doing some research and don't see an example for my specific use case. So general architecture advice/resources would be appreciated.

I'm creating a address book.

  • Users create contacts and groups.
  • Created contacts live in a general pool.
  • Contacts can be associated with multiple groups.
  • If a contact is updated, it's updated across all groups.
  • If a contact is removed from the general pool, it's removed from all associated groups.

Creating separate collections for the contacts and groups was easy, but now I'm kind of stuck.

It appears that Backbone-associations or Backbone-relational or even nesting.js would provide what I'm looking for. Looks like Backbone-associations is more performant than Backbone-relational. Does anybody have experience with any of the plugins? Is there an easier hand-code solution?

like image 839
gojohnnygo Avatar asked Nov 04 '22 06:11

gojohnnygo


1 Answers

Could you have a backbone model representing a group and a collection. The group could contain an array of contactIds:

var Contact = Backbone.Model.extend({
    defaults: {
        name: "SomeName",
        phone: "1234567890",
        ...
    }
});

var Group = Backbone.Model.extend({
    defaults: {
       name: "someName",
       contactIds: [1, 2, 3, 4]
    }
});

var Contacts = Backbone.Collection.extend({
    model: Contact
});

If you need to access the details of a contact stored in a given group, you could do something like:

contacts.get(id); 

to return the individual contact's information for a CRUD operation. You could similarly maintain a collection of groups.

like image 116
Mark Roper Avatar answered Nov 09 '22 05:11

Mark Roper