New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

knockout-collection

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knockout-collection - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

.travis.yml

19

index.js
define(['knockout'], function(ko) {
// Interface: http://www.doctrine-project.org/api/common/2.5/class-Doctrine.Common.Collections.Collection.html
return function KnockoutCollection(items, options) {
var that = this;
this.items = ko.observableArray(items);
if (options && options.reference === true) {
this.items = items;
} else {
this.items = ko.observableArray(items);
}

@@ -45,3 +48,3 @@ this.key = function(item) {

this.removeElement = function(item) {
this.remove = function(item) {
var key = that.key(item);

@@ -58,6 +61,16 @@

this.removeAll = function() {
that.items.removeAll();
};
this.toArray = function() {
return that.items.slice();
};
// initialize corerctly, because the first subscription will not be triggered
this.length = that.items().length;
that.items.subscribe(function(items) {
that.length = items.length;
});
};
});

14

package.json
{
"name": "knockout-collection",
"version": "1.0.0",
"version": "2.0.0",
"description": "A knockout observable array as hashmap",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha tests/js/mocha"
},

@@ -24,3 +24,11 @@ "repository": {

},
"homepage": "https://github.com/webforge-labs/knockout-collection#readme"
"homepage": "https://github.com/webforge-labs/knockout-collection#readme",
"devDependencies": {
"chai": "^3.4.1",
"knockout": "^3.4.0",
"requirejs": "^2.1.22"
},
"dependencies": {
"knockout-mapping": "^2.5.0"
}
}

@@ -1,2 +0,86 @@

# knockout-collection
# knockout-collection [![Build Status](https://travis-ci.org/webforge-labs/knockout-collection.svg?branch=master)](https://travis-ci.org/webforge-labs/knockout-collection)
A knockout observable array as hashmap
## usage
```js
var item1 = {
id: ko.observable(1),
label: 'Item 1'
};
var item2 = {
id: ko.observable(2),
label: 'Item 2'
}
var collection = new KnockoutCollection([item1], { key: 'id' });
expect(collection.get(2)).to.be.undefined;
collection.add(item2);
expect(collection.get(2)).to.have.property('label', 'Item 2');
collection.remove(item1);
collection.remove(item2);
expect(collection.toArray()).to.have.length(0);
```
## api
If you need direct access to the underlying `ko.observableArray` you can use `collection.items`. Use this only to bind, not to modify.
### construction
You can pass an array as items.
```js
new KnockoutCollection(['my', 'array', 'items']);
```
If you want to manage an `ko.observableArray` you can pass it as `items` and set the option: `reference`:
```js
var items = ko.observableArray([item1, item2]);
var collection = new KnockoutCollection(items, { key: 'id', reference: true });
collection.remove(item1);
// items() will be [item2]
```
#### .add(item)
adds an item to the collection which is identified by the value of the (observable-)property with name `options.key`.
**The item is only added, if it isn't already contained in the collection**
#### .remove(item)
removes the item from the collection. If it isnt available in the collection nothing is done
#### .get(keyValue)
returns the item with the value of the (observable-)property with name `options.key` equal to `keyValue`.
it will return `undefined` if the collection does not contain the item.
#### .contains(item)
checks if the item is contained in the collection.
#### .length
You can use the length property as you would use it for an array:
```js
var collection = new KnockoutCollection(['i1', 'i2']);
console.log(collection.length === 2); // is true
```
#### .removeAll()
Removes all items from the collection
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc