{"id":20617077,"url":"https://github.com/kjantzer/backbone-attribute-types","last_synced_at":"2026-05-26T13:37:53.538Z","repository":{"id":143848429,"uuid":"117720605","full_name":"kjantzer/backbone-attribute-types","owner":"kjantzer","description":"A non-obtrusive plugin that extends models to force `get()`ing of attributes to return values in a specific type (integer, date, etc).","archived":false,"fork":false,"pushed_at":"2018-01-16T18:02:24.000Z","size":3,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-18T06:36:49.893Z","etag":null,"topics":["backbone","backbone-models","backbonejs","javascript","javascript-library"],"latest_commit_sha":null,"homepage":null,"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/kjantzer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-16T18:01:36.000Z","updated_at":"2023-03-08T04:37:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"99088c13-34e7-487d-b367-82991ae43941","html_url":"https://github.com/kjantzer/backbone-attribute-types","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/kjantzer%2Fbackbone-attribute-types","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kjantzer%2Fbackbone-attribute-types/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kjantzer%2Fbackbone-attribute-types/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kjantzer%2Fbackbone-attribute-types/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kjantzer","download_url":"https://codeload.github.com/kjantzer/backbone-attribute-types/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242269714,"owners_count":20100159,"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":["backbone","backbone-models","backbonejs","javascript","javascript-library"],"created_at":"2024-11-16T11:22:03.823Z","updated_at":"2026-05-26T13:37:53.504Z","avatar_url":"https://github.com/kjantzer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Backbone Attribute Types\n==============================\n\n![Version 0.1.1](https://img.shields.io/badge/Version-0.1.1-blue.svg)\n\n\u003e A non-obtrusive plugin that extends models to force `get()`ing of attributes to return values in a specific type (integer, date, etc).\n\n## Example Usage\n\n```js\nvar TypedModel = Backbone.Model.extend({\n\n\tattrTypes: {\n\t\t'count': 'int',\n\t\t'runtime': 'float',\n\t\t'created_at': 'moment'\n\t}\n\n});\n\nvar typedModel = new TypedModel({\n\tcount: '10',\n\truntime: '4.75',\n\tcreated_at: '2017-09-19'\n})\n\ntypedModel.get('count') // 10\ntypedModel.get('runtime') // 4.75\ntypedModel.get('created_at') // date object\n\n// a use-case\ntypedModel.attributes['count'] + 9 // = \"109\" (this would normally happen)\ntypedModel.get('count') + 9 // = 19\n```\n\n### Attribute Types `attrTypes`\n\nThere are default attribute types under `Backbone.ModelAttrTypes`. You can add to this or specify a custom type on the model.\n\n\u003e attrTypes can also be defined on the Collection.\n\n\n#### Supported Types\n\n```js\nwindow.Backbone.ModelAttrTypes = {\n\t'string': function(val){ return String(val) },\n\t'bool': function(val){ return !!val },\n\t'int': function(val){ return parseInt(val) },\n\t'float': function(val){ return parseFloat(val) },\n\t'num': function(val){ return parseFloat(val) }, // alias for float\n\t'date': function(val){ return new Date(val) },\n\t// moment.js support\n\t'moment': function(val){ return window.moment ? moment(val) : new Date(val) }\n}\n```\n\n#### Custom Types\n\n```js\nvar CustomTypedModel = Backbone.Model.extend({\n\n\tattrTypes: {\n\t\t'attrName': function(val){\n\t\t\t// convert val to type\n\t\t\treturn val\n\t\t}\n\t}\n\n});\n```\n\n### Casting to type\n\nYou can cast an attribute to a type on the fly by appending a pipe `|` and the type at the end of the attribute name.\n\n```js\ntypedModel.get('runtime') // 4.75\ntypedModel.get('runtime|int') // 4\ntypedModel.get('runtime|string') // \"4.75\"\n```\n\nYou can also cast as `raw` to get the original value stored in `attributes`\n\n```js\ntypedModel.get('runtime|raw') // \"4.75\"\n```\n\n## Why?\n\nThe reason for this plugin is for when your model has an attribute in a string format (for JSON and database consistency) but you wish to use the value in code as a certain type. Rather than 1) continually converting the value to your desired type or 2) creating a secondary method to make this conversion, this plugin will do it for you.\n\nIt's designed to be non-obtrusive by refraining from overwriting the real value in `attributes` and allows you to continue using `.get(attr)` like normal.\n\n## License\n\nMIT © [Kevin Jantzer](http://kevinjantzer.com)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkjantzer%2Fbackbone-attribute-types","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkjantzer%2Fbackbone-attribute-types","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkjantzer%2Fbackbone-attribute-types/lists"}