backbone.relations

The plugin is for defining relations between models.

[npm-badge]: https://badge.fury.io/js/backbone.relations.png [npm-link]: https://badge.fury.io/js/backbone.relations

Backbone.Relations

[![NPM Version][npm-badge]][npm-link]

The plugin is for defining relations between models.

Dependencies:

var User = Backbone.Model.extend({
        initializefunction () {
            this.hasOne(Mailbox, { as: 'mailbox', foreignKey: 'userId' });
        }
    }),
 
    Mailbox = Backbone.Model.extend({
        initializefunction () {
            this.belongsTo(User, { as: 'user', foreignKey: 'userId' });
            this.hasMany(Message, { as: 'messages', foreignKey: 'mailboxId' });
        }
    }),
 
    Message = Backbone.Model.extend({
        initializefunction () {
            this.belongsTo(Mailbox, { as: 'mailbox', foreignKey: 'mailboxId' });
        }
    });
var UserList = Backbone.Collection.extend({
        model: User
    }),
 
    MailboxList = Backbone.Collection.extend({
        model: Mailbox
    }),
 
    MessageList = Backbone.Collection.extend({
        model: Message
    });
var userList = new UserList([
        { id: 1, name: 'Dmytro Nemoga' },
        { id: 2, name: 'Andriy Serputko' }
    ]),
 
    mailboxList = new MailboxList([
        { id: 1, email: 'dnemoga@gmail.com', userId: 1 },
        { id: 2, email: 'aserput@gmail.com', userId: 2 }
    ]),
 
    messageList = new MessageList([
        { id: 1, text: 'Hi, Dmytro! How are you?', mailboxId: 1 },
        { id: 2, text: 'Hi, Andriy! I\'m fine, thanks!', mailboxId: 2 }
    ]);
var user = userList.get(1);
 
user.getMailbox();
user.setMailbox();
user.buildMailbox();
user.createMailbox();
var mailbox = mailboxList.get(1);
 
mailbox.getMessages();
var message = messageList.get(1);
 
message.getMailbox();
message.setMailbox();
message.buildMailbox();
message.createMailbox();
  • Removed Underscore.String depenedency
  • Removed method toJSON
  • Removed CommonJS support
  • Added CommonJS support
  • Option advanced of toJSON method's renamed to relations
  • Reverted method toJSON
  • Removed method toJSON
  • Initial release