{"id":20947717,"url":"https://github.com/alphahydrae/backbone-relational-hal","last_synced_at":"2025-05-14T02:30:28.053Z","repository":{"id":14607771,"uuid":"17324986","full_name":"AlphaHydrae/backbone-relational-hal","owner":"AlphaHydrae","description":"Consume HAL+JSON APIs with Backbone.js and Backbone-relational.js","archived":false,"fork":false,"pushed_at":"2014-10-24T08:07:00.000Z","size":387,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-11T10:44:02.166Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"aws/aws-sdk-go","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlphaHydrae.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-01T21:41:44.000Z","updated_at":"2019-09-02T19:08:39.000Z","dependencies_parsed_at":"2022-09-11T07:51:25.785Z","dependency_job_id":null,"html_url":"https://github.com/AlphaHydrae/backbone-relational-hal","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlphaHydrae%2Fbackbone-relational-hal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlphaHydrae%2Fbackbone-relational-hal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlphaHydrae%2Fbackbone-relational-hal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlphaHydrae%2Fbackbone-relational-hal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlphaHydrae","download_url":"https://codeload.github.com/AlphaHydrae/backbone-relational-hal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225270612,"owners_count":17447635,"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-19T00:12:47.037Z","updated_at":"2024-11-19T00:12:47.682Z","avatar_url":"https://github.com/AlphaHydrae.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# backbone-relational-hal\n\n**Consume [HAL+JSON](http://stateless.co/hal_specification.html) APIs  with [Backbone.js](http://backbonejs.org) and [Backbone-relational.js](https://github.com/PaulUithol/Backbone-relational).**\n\n[![NPM version](https://badge.fury.io/js/backbone-relational-hal.png)](http://badge.fury.io/js/backbone-relational-hal)\n[![Build Status](https://secure.travis-ci.org/AlphaHydrae/backbone-relational-hal.png)](http://travis-ci.org/AlphaHydrae/backbone-relational-hal)\n\n## Requirements\n\n**backbone-relational-hal** is currently tested with the following libraries:\n\n* [Underscore](http://underscorejs.org) v1.7.0\n* [jQuery](http://jquery.com) v2.1.1\n* [uri-templates](https://github.com/geraintluff/uri-templates) v0.1.5\n* [Backbone.js](http://backbonejs.org) v1.1.2\n* [Backbone-relational.js](http://backbonerelational.org) v0.8.8\n\n## Installation\n\nWith bower:\n\n    bower install --save backbone-relational-hal\n\nBuilds:\n\n* Development: [backbone-relational-hal.js](https://raw.github.com/AlphaHydrae/backbone-relational-hal/master/backbone-relational-hal.js)\n* Production: [backbone-relational-hal.min.js](https://raw.github.com/AlphaHydrae/backbone-relational-hal/master/backbone-relational-hal.min.js)\n\n## Usage\n\nThis library adds a new `Backbone.RelationalHalResource` class that you can extend like `Backbone.RelationalModel` or `Backbone.Model`.\n\n```js\nvar Person = Backbone.RelationalHalResource.extend({\n  url: '/people/2'\n});\n```\n\n**HAL Links**\n\n* [Hyperlinks](#hyperlinks)\n* [URI templates](#uri-templates)\n* [Generating \u0026lt;a\u0026gt; tags from links](#generating-a-tags-from-links)\n\n**HAL Embedded Resources**\n\n* [Embedding resources](#embedding-resources)\n\n### Hyperlinks\n\nHAL resources can parse HAL+JSON links out of the box.\n\nLet's instantiate the above sample class and fetch data from the server.\n\n```js\nvar person = new Person();\nperson.fetch();\n```\n\nAssume the server would return this data containing several links.\n\n```json\n{\n  \"_links\": {\n    \"self\": { \"href\": '/people/2' },\n    \"search\": { \"href\": '/people/2/search{?email}', \"templated\": true },\n    \"alternate\": [\n      { \"href\": '/people/2.html', \"type\": 'text/html' },\n      { \"href\": '/people/2.xml', \"type\": 'application/xml' }\n    ]\n  },\n  \"name\": \"Nobody\"\n}\n```\n\nUse the `link` method of a HAL resource to retrieve useful link objects.\n\n```js\n// get a link object\nperson.link('self'); // =\u003e a Backbone.RelationalModel\n\n// get the href of a link\nperson.link('self').href(); // =\u003e '/people/2'\n\n// get a list of links\nvar alternates = person.link('alternate', { all: true }); // =\u003e Backbone.Collection of link objects\n\n// get one of the links in the list\nalternates.at(0).href(); // =\u003e '/people/2.html'\n\n// find a link matching criteria\n// (using Backbone's Underscore proxy methods)\nalternates.findWhere({ type: 'application/xml' }).href(); // =\u003e '/people/2.xml'\n```\n\n### URI Templates\n\nTemplated links can be expanded using the [uri-templates](https://github.com/geraintluff/uri-templates) library.\n\n```js\n// get a templated link without parameters\nperson.link('search').href(); // =\u003e '/people/2/search'\n\n// expand a link template with parameters\nperson.link('search').href({ template: { email: 'foo@example.com' } }); // =\u003e '/people/2/search?email=foo@example.com'\n```\n\n### Generating \u0026lt;a\u0026gt; Tags from Links\n\nThe `tag` method of link objects can generate HTML link tags for you.\n\n```js\n// simple tag with text content\nperson.link('self').tag('John'); // =\u003e $('\u003ca href=\"/people/2\"\u003eJohn\u003c/a\u003e')\n\n// HTML content\nvar content = $('\u003cstrong /\u003e').text('Jane');\nperson.link('self').tag(content, { html: true }); // =\u003e $('\u003ca href=\"/people/2\"\u003e\u003cstrong\u003eJane\u003c/strong\u003e\u003c/a\u003e')\n```\n\n### Embedding Resources\n\nDefine embedded resources with the `halEmbedded` property when extending a HAL resource.\nEmbedded resources are Backbone-relational.js relations and use the same options.\n\n```js\nvar Company = Backbone.RelationalHalResource.extend({\n\n  halEmbedded: [\n    {\n      type: Backbone.HasOne,\n      key: 'http://example.com/rel/manager',\n      relatedModel: Person\n    },\n    {\n      type: Backbone.HasMany,\n      key: 'http://example.com/rel/employees',\n      relatedModel: Person\n    }\n  ]\n});\n```\n\nYou can also use an equivalent HAL-like syntax where the key in the `halEmbedded` object is automatically used as the relation key.\n\n```js\nvar Company = Backbone.RelationalHalResource.extend({\n\n  halEmbedded: {\n    'http://example.com/rel/manager': {\n      type: Backbone.HasOne,\n      relatedModel: Person\n    },\n    'http://example.com/rel/employees': {\n      type: Backbone.HasMany,\n      relatedModel: Person\n    }\n  }\n});\n```\n\nAssume the server would return this data for a company.\n\n```json\n{\n  \"name\": \"Initech\",\n  \"_embedded\": {\n    \"http://example.com/rel/manager\": {\n      \"name\": \"Bill Lumbergh\"\n    },\n    \"http://example.com/rel/employees\": [\n      {\n        \"name\": \"Peter Gibbons\"\n      },\n      {\n        \"name\": \"Michael Bolton\"\n      },\n      {\n        \"name\": \"Samir Nagheenanajar\"\n      }\n    ]\n  }\n}\n```\n\nYou can retrieve embedded resources with the `embedded` method.\n\n```js\n// get an embedded resource\ncompany.embedded('http://example.com/rel/manager'); // =\u003e Person\ncompany.embedded('http://example.com/rel/manager').get('name'); // =\u003e 'Bill Lumbergh'\n\n// get a Backbone.Collection of embedded resources\nvar employees = company.embedded('http://example.com/rel/employees'); // =\u003e Backbone.Collection\n\n// get one of the resources in the collection\nemployees.at(0); // =\u003e Person\nemployees.at(0).get('name'); // =\u003e 'Peter Gibbons'\n```\n\n## Meta\n\n* **Author:** Simon Oulevay (Alpha Hydrae)\n* **License:** MIT (see [LICENSE.txt](https://raw.github.com/AlphaHydrae/backbone-relational-hal/master/LICENSE.txt))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falphahydrae%2Fbackbone-relational-hal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falphahydrae%2Fbackbone-relational-hal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falphahydrae%2Fbackbone-relational-hal/lists"}