{"id":19793549,"url":"https://github.com/darky/backbone.linear","last_synced_at":"2025-05-01T02:30:42.898Z","repository":{"id":17907068,"uuid":"20865171","full_name":"darky/backbone.linear","owner":"darky","description":"Easiest way to work with your Backbone.Model nested array-object attributes.","archived":false,"fork":false,"pushed_at":"2015-08-04T15:17:25.000Z","size":820,"stargazers_count":27,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-25T21:52:18.296Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/darky.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":"2014-06-15T20:57:41.000Z","updated_at":"2022-06-15T11:55:23.000Z","dependencies_parsed_at":"2022-09-02T04:23:38.748Z","dependency_job_id":null,"html_url":"https://github.com/darky/backbone.linear","commit_stats":null,"previous_names":["darrrk/backbone.linear"],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fbackbone.linear","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fbackbone.linear/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fbackbone.linear/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fbackbone.linear/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darky","download_url":"https://codeload.github.com/darky/backbone.linear/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251812261,"owners_count":21647875,"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-12T07:10:19.480Z","updated_at":"2025-05-01T02:30:42.455Z","avatar_url":"https://github.com/darky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Backbone.Linear\n\n[![](https://travis-ci.org/darrrk/backbone.linear.svg?branch=master)](https://travis-ci.org/darrrk/backbone.linear)\n\nEasiest way to work with your Backbone.Model nested array-object attributes.\n\n## Idea\n\nUnlike other similar libraries, `Backbone.Linear` not fighting versus Backbone API and hardly rewrite them.\n`Backbone.Linear` - little extension, that extend only `parse` and `sync` methods in `Backbone.Model`\nFor example if server responds:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 4\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n    \nIn `parse` method it transforms in:\n\n```json\n{\n    \"Cats.Boris.age\": 3,\n    \"Cats.Boris.weight\": 4,\n    \"Cats.Milla.age\": 1,\n    \"Cats.Milla.weight\": 2\n}\n```\n    \nAnd you can use all Backbone API power with linear attributes.\nThen you save it back to server, it transforms backward to:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 8,\n            \"note\": \"Oh, Boris look plumped\"\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n\n## Simple using\n\nAll similar `Backbone`, only using `Backbone.LinearModel` class\n\n```javascript\nvar MyLinearModelClass = Backbone.LinearModel.extend(\n    // bla-bla\n)\n```\n    \n## Extend using\n\nYou can define `flatOptions` settings (object or function, that return object) to manipulate server \u003c-\u003e client transform behavior:\n\n### delimiter (default: \".\")\n\n```javascript\nvar MyLinearModelClass = Backbone.LinearModel.extend({\n    flatOptions : {\n        delimiter : \"-\"\n    }\n});\n```\n    \nServer response:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 4\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n    \nTransforms to:\n\n```json\n{\n    \"Cats-Boris-age\": 3,\n    \"Cats-Boris-weight\": 4,\n    \"Cats-Milla-age\": 1,\n    \"Cats-Milla-weight\": 2\n}\n```\n    \nAnd vice versa...\n\n### safe (default: true)\n    \nBy default server response:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 4,\n            \"toys\": [\n                \"ball\",\n                \"mouse\"\n            ]\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n    \nTransforms with preservation array structure:\n\n```json\n{\n    \"Cats.Boris.age\": 3,\n    \"Cats.Boris.weight\": 4,\n    \"Cats.Boris.toys\": [\"ball\", \"mouse\"],\n    \"Cats.Milla.age\": 1,\n    \"Cats.Milla.weight\": 2\n}\n```\n    \nUse `safe : false`:\n\n```javascript\nvar MyLinearModelClass = Backbone.LinearModel.extend({\n    flatOptions : {\n        safe : false\n    }\n});\n```\n\nAnd server response will be with objectify arrays:\n\n```json\n{\n    \"Cats.Boris.age\": 3,\n    \"Cats.Boris.weight\": 4,\n    \"Cats.Boris.toys.0\": \"ball\",\n    \"Cats.Boris.toys.1\": \"mouse\",\n    \"Cats.Milla.age\": 1,\n    \"Cats.Milla.weight\": 2\n}\n```\n    \n### object (default: false)\n\n```javascript\nvar MyLinearModelClass = Backbone.LinearModel.extend({\n    flatOptions : {\n        object : true\n    }\n});\n```\n\nWhen your client data:\n\n```json\n{\n    \"Cats.Boris.age\": 3,\n    \"Cats.Boris.weight\": 4,\n    \"Cats.Boris.toys.0\": \"ball\",\n    \"Cats.Boris.toys.1\": \"mouse\",\n    \"Cats.Milla.age\": 1,\n    \"Cats.Milla.weight\": 2\n}\n```\n    \nWill save on server, array not be created automatically:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 4,\n            \"toys\": {\n                \"0\": \"ball\",\n                \"1\": \"mouse\"\n            }\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n    \nInstead:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 4,\n            \"toys\": [\n                \"ball\",\n                \"mouse\"\n            ]\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n\n### overwrite (default: false)\n\n```javascript\nvar MyLinearModelClass = Backbone.LinearModel.extend({\n    flatOptions : {\n        overwrite : true\n    }\n});\n```\n\nWhen your client data:\n\n```json\n{\n    \"Cats.Boris.toys\": true,\n    \"Cats.Boris.toys.0\": \"ball\",\n    \"Cats.Boris.toys.1\": \"mouse\"\n}\n```\n    \nWill save on server - internal array, with collision by key, will be included:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"toys\" : [\n                \"ball\",\n                \"mouse\"\n            ]\n        }\n    }\n}\n```\n    \nInstead:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"toys\" : true\n        }\n    }\n}\n```\n\n### maxDepth (default: undefined)\n\n```javascript\nvar MyLinearModelClass = Backbone.LinearModel.extend({\n    flatOptions : {\n        maxDepth : 2\n    }\n});\n```\n\nServer response:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 4\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n    \nTransforms to:\n\n```json\n{\n    \"Cats.Boris\": {\n        \"age\": 3,\n        \"weight\": 4\n    },\n    \"Cats.Milla\": {\n        \"age\": 1,\n        \"weight\": 2\n    }\n}\n```\n\n### forceArray (default: undefined)\n\n```javascript\nvar MyLinearModelClass = Backbone.LinearModel.extend({\n    flatOptions : {\n        forceArray : [\n            \"Cats.Boris.age\",\n            \"Cats.Boris.eyes\",\n            \"Cats.Boris.toys\",\n            \"Cats.Milla.toys\"\n        ]\n    }\n});\n```\n    \nAnd server response:\n\n```json\n{\n    \"Cats\": {\n        \"Boris\": {\n            \"age\": 3,\n            \"weight\": 4,\n            \"toys\": {\n                \"item\": \"ball\"\n            },\n            \"eyes\": [\n                \"left_eye\"\n                \"right_eye\"\n            ]\n        },\n        \"Milla\": {\n            \"age\": 1,\n            \"weight\": 2\n        }\n    }\n}\n```\n    \nGuarantees the creation of array:\n\n```json\n{\n    \"Cats.Boris.age\": [3],\n    \"Cats.Boris.weight\": 4,\n    \"Cats.Boris.toys\": [{\"item\": \"ball\"}],\n    \"Cats.Boris.eyes\": [\"left_eye\", \"right_eye\"],\n    \"Cats.Milla.age\": 1,\n    \"Cats.Milla.weight\": 2,\n    \"Cats.Milla.toys\": []\n}\n```\n    \n**Note: When using `forceArray` - option `safe` force set to `true`**\n\n## Helpers\n\nYou can manually flatten, unflatten objects via static helpers:\n\n```javascript\nBackbone.LinearModel.flatten(target, options);\nBackbone.LinearModel.unflatten(target, options);\n```\n\nAs options you can use `delimiter`, `safe`, `object`, that described above\n\n## Dependencies\n\n```coffeescript\nswitch Backbone.VERSION\n    when \"1.1.2\", \"1.2.0\"\n        \"Yep, all tests passing\"\n    else\n        \"Maybe, but not testing\"\n```\n\n## License \n\n(The MIT License)\n\nCopyright (c) 2014-2015 Vladislav Botvin \u0026lt;darkvlados@gmail.com\u0026gt;\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n\n### License of Flat\n\nCopyright (c) 2014, Hugh Kennedy\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\n2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\n3. Neither the name of the  nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n\nHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\nINCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\nIN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\nEXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\nWHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\nARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Fbackbone.linear","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarky%2Fbackbone.linear","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Fbackbone.linear/lists"}