{"id":22862217,"url":"https://github.com/webforge-labs/knockout-collection","last_synced_at":"2025-03-31T08:45:26.453Z","repository":{"id":66175664,"uuid":"47920260","full_name":"webforge-labs/knockout-collection","owner":"webforge-labs","description":"A knockout observable array as hashmap","archived":false,"fork":false,"pushed_at":"2016-05-06T07:45:51.000Z","size":9,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T04:44:55.359Z","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/webforge-labs.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-13T13:26:56.000Z","updated_at":"2018-03-30T01:51:52.000Z","dependencies_parsed_at":"2023-02-20T23:16:08.298Z","dependency_job_id":null,"html_url":"https://github.com/webforge-labs/knockout-collection","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webforge-labs%2Fknockout-collection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webforge-labs%2Fknockout-collection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webforge-labs%2Fknockout-collection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webforge-labs%2Fknockout-collection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webforge-labs","download_url":"https://codeload.github.com/webforge-labs/knockout-collection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246443524,"owners_count":20778247,"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-12-13T10:12:40.502Z","updated_at":"2025-03-31T08:45:26.433Z","avatar_url":"https://github.com/webforge-labs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n# knockout-collection [![Build Status](https://travis-ci.org/webforge-labs/knockout-collection.svg?branch=master)](https://travis-ci.org/webforge-labs/knockout-collection)\n\nA knockout observable array as hashmap\n\n## usage\n\n```js\nvar item1 = {\n  id: ko.observable(1),\n  label: 'Item 1'\n};\n\nvar item2 = {\n  id: ko.observable(2),\n  label: 'Item 2'\n}\n\nvar collection = new KnockoutCollection([item1], { key: 'id' });\n\nexpect(collection.get(2)).to.be.undefined;\n\ncollection.add(item2);\n\nexpect(collection.get(2)).to.have.property('label', 'Item 2');\n\ncollection.remove(item1);\ncollection.remove(item2);\n\nexpect(collection.toArray()).to.have.length(0);\n```\n\n## api\n\nIf you need direct access to the underlying `ko.observableArray` you can use `collection.items`. Use this only to bind, not to modify.\n\n### construction\n\nYou can pass an array as items.\n```js\nnew KnockoutCollection(['my', 'array', 'items']);\n```\n\nIf you want to manage an `ko.observableArray` you can pass it as `items` and set the option: `reference`:\n\n```js\nvar items = ko.observableArray([item1, item2]);\nvar collection = new KnockoutCollection(items, { key: 'id', reference: true });\n\ncollection.remove(item1);\n\n// items() will be [item2]\n```\n\n#### .add(item)\n\nadds an item to the collection which is identified by the value of the (observable-)property with name `options.key`.  \n**The item is only added, if it isn't already contained in the collection**\n\n#### .remove(item)\n\nremoves the item from the collection. If it isnt available in the collection nothing is done\n\n#### .get(keyValue)\n\nreturns the item with the value of the (observable-)property with name `options.key` equal to `keyValue`.  \nit will return `undefined` if the collection does not contain the item.\n\n#### .contains(item)\n\nchecks if the item is contained in the collection.\n\n#### .length\n\nYou can use the length property as you would use it for an array:\n\n```js\nvar collection = new KnockoutCollection(['i1', 'i2']);\n\nconsole.log(collection.length === 2); // is true\n```\n\n#### .removeAll()\n\nRemoves all items from the collection\n#### .toArray()\n\nreturns an array with all items of the collection. The array is a copy.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebforge-labs%2Fknockout-collection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebforge-labs%2Fknockout-collection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebforge-labs%2Fknockout-collection/lists"}