{"comment":{"created_at":"2011-10-18T21:52:47Z","body":"Thank you, Paul.\r\nI agree that your variant is indeed more logical.\r\n\r\nBut what about uniqueness of jobs?  I want the <code>jobs</code> collection to operate like a Python set.","updated_at":"2011-10-18T21:52:47Z","url":"https://api.github.com/repos/PaulUithol/Backbone-relational/issues/comments/2448364","id":2448364,"user":{"gravatar_id":"812c911a7fede24c46ec8a625e6d5e45","avatar_url":"https://secure.gravatar.com/avatar/812c911a7fede24c46ec8a625e6d5e45?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/avrelian","id":1076352,"login":"avrelian"}},"action":"created","issue":{"number":32,"created_at":"2011-09-25T14:24:35Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"Hello, Paul!\r\n\r\nConsidering your example with Person-Job-Company Many-to-Many relation, I can implement uniqueness of jobs and 2-way messaging between Person and Company objects linked by Job object only with a bunch of code during Job object initialization. Is it possible to do these tasks in a cleaner way?\r\n\r\nMay be it was just my case, but Company object added to 'jobs' collection of Person object triggers not an 'update:company' event but 'update:person' when being changed. And vice versa.\r\n\r\n```javascript\r\nPerson = Backbone.RelationalModel.extend({\r\n    initialize: function(){\r\n        this.bind('update:jobs', function(company) {\r\n            alert(this.get('name') + ' says: \"My company has been updated to ' + company.get('name') + '\"');\r\n        }, this);\r\n    },\r\n    relations: [\r\n        {\r\n            type: 'HasMany',\r\n            key: 'jobs',\r\n            relatedModel: 'Job',\r\n            reverseRelation: {\r\n                key: 'person'\r\n            }\r\n        }\r\n    ],\r\n    getCompanies: function(){\r\n        return this.get('jobs').pluck('company');\r\n    }\r\n});\r\n\r\nJob = Backbone.RelationalModel.extend({\r\n    initialize: function(){\r\n        this.bind('add', function(job, jobs){\r\n            var person = job.get('person');\r\n            var company = job.get('company');\r\n            \r\n            if(person && company){       \r\n                // uniqueness of jobs\r\n                if( ! this.isUnique(jobs, person, company)){\r\n                    this.del(job, person);\r\n                } else {\r\n                    // 2-way messaging\r\n                    person.bind('change', function(){\r\n                        company.trigger('update:employees', person);\r\n                    }, company);\r\n                    company.bind('change', function(){\r\n                        person.trigger('update:jobs', company);\r\n                    }, person);\r\n                }\r\n            }\r\n        }, this);\r\n    },\r\n    del: function(job, person){\r\n        setTimeout(function(){\r\n            person.get('jobs').remove([job]);\r\n        }, 100);\r\n    },\r\n    isUnique: function(jobs, person, company){\r\n        return _(jobs.models).chain().select(function(job){\r\n            return job.get('company') === company && job.get('person') === person;\r\n        }).size().value() == 1;\r\n    }\r\n});\r\n\r\nCompany = Backbone.RelationalModel.extend({\r\n    initialize: function(){\r\n        this.bind('update:employees', function(person) {\r\n            alert(this.get('name') + ' says: \"My employee has been updated to ' + person.get('name') + '\"');\r\n        }, this);\r\n    },\r\n    relations: [\r\n        {\r\n            type: 'HasMany',\r\n            key: 'employees',\r\n            relatedModel: 'Job',\r\n            reverseRelation: {\r\n                key: 'company'\r\n            }\r\n        }\r\n    ],\r\n    getPersons: function(){\r\n        return this.get('employees').pluck('person');\r\n    }\r\n});\r\n\r\nibm = new Company({\r\n    id: 1, \r\n    name: 'IBM'\r\n});\r\nbmw = new Company({\r\n    id: 2,\r\n    name: 'BMW'\r\n});\r\npepsi = new Company({\r\n    id: 3,\r\n    name: 'Pepsi'\r\n});\r\n\r\nme = new Person({\r\n    id: 1, \r\n    name:'Old Me'\r\n});\r\n\r\nme.get('jobs').add([{company: ibm}, {company: bmw}, {company: pepsi}, {company: pepsi}, {company: pepsi}]);\r\nibm.get('employees').add([{person: me}]);\r\nbmw.get('employees').add([{person: me}]);\r\npepsi.get('employees').add([{person: me}]);\r\n\r\nme.set({name: 'New Me'});\r\npepsi.set({name: 'New Pepsi'});\r\n```\r\n\r\nThis is a link to jsFiddle with the code http://jsfiddle.net/avrelian/Z4a65/","comments":2,"title":"Uniqueness and 2-way messaging for Many-to-Many","updated_at":"2011-10-18T21:52:47Z","url":"https://api.github.com/repos/PaulUithol/Backbone-relational/issues/32","id":1733646,"assignee":null,"milestone":null,"closed_at":"2011-10-18T17:08:19Z","user":{"gravatar_id":"812c911a7fede24c46ec8a625e6d5e45","avatar_url":"https://secure.gravatar.com/avatar/812c911a7fede24c46ec8a625e6d5e45?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","url":"https://api.github.com/users/avrelian","id":1076352,"login":"avrelian"},"html_url":"https://github.com/PaulUithol/Backbone-relational/issues/32","labels":[],"state":"closed"},"legacy":{"issue_id":1733646,"comment_id":2448364}}