{"id":20525982,"url":"https://github.com/codeyellowbv/backbone-relation","last_synced_at":"2025-04-14T04:09:37.582Z","repository":{"id":57189067,"uuid":"43944951","full_name":"CodeYellowBV/backbone-relation","owner":"CodeYellowBV","description":"Simple support for relations for Backbone models","archived":false,"fork":false,"pushed_at":"2017-03-15T18:58:54.000Z","size":78,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-14T04:09:37.334Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CodeYellowBV.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-09T09:10:23.000Z","updated_at":"2018-07-09T07:04:41.000Z","dependencies_parsed_at":"2022-09-15T03:50:16.031Z","dependency_job_id":null,"html_url":"https://github.com/CodeYellowBV/backbone-relation","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeYellowBV%2Fbackbone-relation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeYellowBV%2Fbackbone-relation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeYellowBV%2Fbackbone-relation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodeYellowBV%2Fbackbone-relation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CodeYellowBV","download_url":"https://codeload.github.com/CodeYellowBV/backbone-relation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248819403,"owners_count":21166477,"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-15T23:11:41.285Z","updated_at":"2025-04-14T04:09:37.542Z","avatar_url":"https://github.com/CodeYellowBV.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# backbone-relation\n\n[![Build Status](https://travis-ci.org/CodeYellowBV/backbone-relation.svg?branch=master)](https://travis-ci.org/CodeYellowBV/backbone-relation)\n[![Coverage Status](https://coveralls.io/repos/github/CodeYellowBV/backbone-relation/badge.svg?branch=master)](https://coveralls.io/github/CodeYellowBV/backbone-relation?branch=master)\n\nBackbone does not support relations by default. This is a simple package that adds relations to Backbone.\n\nThe idea is simple: you can define relations like you define defaults. A relation can be either a Model or a Collection and attributes can be set recursively through the parent.\n\nWhy use this over [backbone-relational](http://backbonerelational.org/)?\nWe found backbone-relational too complex. Itʼs basically an ORM in the frontend. We didnʼt need that, hence this package.\n\n## Vision\n\n- 100% compatible with original Backbone codebase. All Backbone test must pass when tested using backbone-relation.\n- Extend Backbone, don't patch it.\n- Make it clear. Only add well defined and tested features, leaving as little as possible open to interpretation.\n\n## Use case\n\nImagine an API-endpoint gives the following response when hitting `api/user/1`:\n\n```\n{\n    id: 1,\n    username: 'witchhunter',\n    profile: {\n        id: 7,\n        first_name: 'Michelle',\n        last_name: 'Velvet'\n    }\n}\n```\n\nYou can define 2 models, one called `MProfile` and the other `MUser`:\n\n```\nMProfile = MRelation.extend();\n\nMUser = MRelation.extend({\n    relations: {\n        profile: MProfile\n    }\n})\n```\n\nNow you can do a fetch and the profile model will be filled with data on the user model:\n\n```\nmUser = new MUser({id: 1});\nmUser.fetch();\n\n... \n\nmUser.get('profile').get('first_name'); // 'Michelle'\n```\n\nWhen setting data, you can do the following:\n\n```\nmUser.set({\n    profile: {first_name: 'Vera'}\n});\n\nmUser.get('profile').get('first_name'); // 'Vera'\n```\n\n## Defining relations\n\nYou can define relations using the `relations` attribute. It can either be a hash or a function that returns a hash. There are a few different ways to define relations:\n\n### Simple\n\nThe simplest form without any options. Example:\n\n```js\nvar MAuthor = Model.extend({\n    relations: {\n        // This is a simple relation where MUser is a Model.\n        user: MUser,\n\n        // Here we define an attribute which should be a collection.\n        posts: CPost,\n    }\n})\n```\n\n### Advanced\n\nWith this form you can add a bit more configuration options. These are the options at the moment:\n\n| Key | Description |\n| --- | ----------- |\n| `relationClass` | The constructor for the relation. This can either be a `Backbone.Model` or a `Backbone.Collection`.\n\nExample:\n\n```js\nvar MAuthor = Model.extend({\n    relations: {\n        // Similar to posts, just another syntax. This supports more complex configuration options.\n        contacts: {relationClass: CContact}\n    }\n})\n```\n\n## Setting related data\n\nThe most basic form of setting a related data is getting it first:\n\n```js\nmAuthor.get('user').set('id', 17);\nmAuthor.get('contacts').set([{id: 1}, {id: 2}]);\n```\n\nAnother way of setting related data is using the attribute name on the parent. These lines do exactly the same:\n\n```js\nmAuthor.get('user').set('id', 17);\nmAuthor.set('user', {id: 17});\nmAuthor.set({user: {id: 17}});\n```\n\nThis implies that once a relation is set, it cannot be overridden with another value. If you do really want to set another instance for a relation, then first use `unset`:\n\n```js\nmAuthor.unset('user');\nmAuthor.set('user', mUser);\n```\n\nBottom line: if a relation exists, `set` is proxied. \n\n## Getting related data\n\nYou can use vanilla Backbone to get related data:\n\n```js\nmAuther.get('user'); // -\u003e Instance of MUser.\nmAuther.get('user').get('id') // -\u003e Get the id of the related user.\n```\n\n### Model.dot\n\nShorthand for getting nested attributes. Example:\n\n```js\nmodel\n    .get('nestedModel1')\n    .get('nestedCollection2')\n    .get('nestedIdOfModel3')\n    .get('foo');\n```\n\ncan be written like:\n\n```js\nmodel.dot('nestedModel1.nestedCollection2.nestedIdOfModel3.foo');\n```\n\nThis depends on that the nested relation has a `get` function defined. That function is called each time a dot is found. If you try to use `dot` on a value that does not have the function `get` defined, it will return `undefined`.\n\nReturns `undefined` because of `someString` is a string without a `get` function defined:\n\n```js\nmodel.dot('nestedModel1.someString.foo.bar');\n```\n\nReturns `undefined` because of `object` is an object without a `get` function defined:\n\n```js\nmodel.dot('nestedModel1.object.foo.bar');\n```\n\nReturns `undefined` because of `nonExistingModelOrCollection` is `undefined` and thus without a `get` function defined:\n\n```js\nmodel.dot('nestedModel1.nonExistingModelOrCollection.foo.bar');\n```\n\nReturns `undefined` because of `nonExistingId` is `undefined` and thus without a `get` function defined:\n\n```js\nmodel.dot('nestedCollection1.nonExistingId.foo.bar');\n```\n\nItʼs not possible to retrieve attributes with a `.` in the name. You can use `get` instead:\n\n```js\nmodel.dot('nestedModel1.nestedCollection2.nestedIdOfModel3').get('foo.bar');\n```\n\n## Options\n\n| Key | Default | Description |\n| --- | ------- | ----------- |\n| createRelations | `true` |  `true`: create relations while initializing model. `false`: skip creating relations upon initialization. |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeyellowbv%2Fbackbone-relation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeyellowbv%2Fbackbone-relation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeyellowbv%2Fbackbone-relation/lists"}