{"id":21326219,"url":"https://github.com/masylum/backbone.rel","last_synced_at":"2025-07-12T06:32:58.486Z","repository":{"id":2895057,"uuid":"3902696","full_name":"masylum/Backbone.Rel","owner":"masylum","description":"Backbone.Rel extends your Backbone models with a lightweight relationships manager.","archived":false,"fork":false,"pushed_at":"2015-09-09T11:28:43.000Z","size":4540,"stargazers_count":144,"open_issues_count":0,"forks_count":10,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-16T00:11:47.962Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/masylum.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-04-02T16:34:30.000Z","updated_at":"2023-12-26T18:19:07.000Z","dependencies_parsed_at":"2022-08-31T04:20:35.049Z","dependency_job_id":null,"html_url":"https://github.com/masylum/Backbone.Rel","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masylum%2FBackbone.Rel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masylum%2FBackbone.Rel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masylum%2FBackbone.Rel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masylum%2FBackbone.Rel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/masylum","download_url":"https://codeload.github.com/masylum/Backbone.Rel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225802658,"owners_count":17526452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-21T21:08:49.517Z","updated_at":"2024-11-21T21:08:50.153Z","avatar_url":"https://github.com/masylum.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Backbone.Rel\n\n![](https://api.travis-ci.org/masylum/Backbone.Rel.svg) \u003ca href='http://redbooth.com' target='_blank'\u003e![](https://www.dropbox.com/s/qo4yp1tpbsvqfya/made-at-redbooth-blue.svg?dl=1)\u003c/a\u003e\n\nBackbone.Rel extends your Backbone models with a lightweight relationships manager.\n\n## How does it work? API\n\n### Rel\n\nBackbone.Rel exposes a method `rel` that is a relationship *getter*.\n\n### hasMany\n\nYou can implement a `hasMany` method in a `Model` to define a relationship.\nThe method must return an object with the relation name as a key and options as a value.\n\nOptions:\n\n  - `collection`: You must specify the collection.\n  - `filter`: A function that will be used to filter the collection.\n  - `id`: The foreign id pointing to your model.\n\n### belongsTo\n\nYou can implement a `belongsTo` method in a `Model` or a `Collection` to define a relationship.\nThe method must return an object with the relation name as a key and the collection as a value.\n\nIf you want to define a `belongsTo` relationship on a collection you have to store the key\non the collection object in your `initialize`.\n\n## Accessing nested relationships, the \"monadic\" `rel` getter.\n\nYou can pass as many arguments as you want to the `rel` getter in order to get nested relationships.\nAny failure on the getter chain will be properly propagated, avoiding `TypeError: Cannot call method 'foo' of null`.\n\n## Accessing attributes inside the relations with `relGet`\n\nMost of the errors on working with relational data is when a relation is not met and we try to access a attribute\nof that inexistant model. For instance, we have a task that belong to a project and we want to get that projects name.\n\n```js\ntask.relGet('project', 'name', 'Unknown project'); // It will show the project name or 'Unkown project'\nuser.relGet('project.tasks', 'name', []); // It will return all tasks names or []\n```\n\n## Invoking methods inside relations\n\nSometimes we want to invoke a method or access an attribute through a relation. `relResult` is a wrapper of `_.result`\non a relation.\n\n```js\ntask.relResult('project', 'fullName', 'Unknown project'); // It will call project.fullName if the relation succeded\n```\n\n## Example\n\n``` javascript\n// models/project.js\nModels.Project.hasMany = function () {\n  return {\n    users: {collection: Collections.users, id: 'project_id'}\n  , tasks: {collection: Collection.tasks, filter: function (task) {\n      return task.rel('project') ? task.rel('project').id === this.id : null;\n    }}\n  };\n};\n\n// models/user.js\nModels.User.hasMany = function () {\n  return {\n    tasks: {collection: Collections.tasks, id: 'user_id'}\n  };\n};\n\nModels.User.belongsTo = function () {\n  return {\n    project: Collection.projects\n  };\n};\n\n// models/task.js\nModels.Task.belongsTo = function () {\n  return {\n    user: Collections.users\n  , project: function (task) {\n      return task.rel('user.project');\n    }\n  };\n};\n\nvar project = new Project({id: 1})\n  , user = new User({id: 1, project_id: 1})\n  , task1 = new Task({id: 1, user_id: 1})\n  , task2 = new Task({id: 2, user_id: 1});\n\nassert.equal(user.rel('tasks').length, 2);\nassert.equal(user.rel('project'), project);\nassert.equal(task1.rel('user'), user);\nassert.equal(task1.rel('project'), project);\n```\n\n## Dependencies\n\nBackbone.Rel depends on [underscore.inflection](https://github.com/jeremyruppel/underscore.inflection).\n\n## Tests\n\nYou must have node installed in order to run the tests.\n\n```\nnpm install\nnpm test\n```\n\n## License\n\n(The MIT License)\n\nCopyright (c) 2012 Pau Ramon \u003cmasylum@gmail.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasylum%2Fbackbone.rel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasylum%2Fbackbone.rel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasylum%2Fbackbone.rel/lists"}