Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-collection-assistant

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-collection-assistant

Helper service to handle Angular collection update events via $watchCollection

  • 1.1.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Angular-Collection-Assistant

Small, dependency free module to help manage collection events when using $watchCollection within Angular.

Installation

Use bower to install into a project:

bower install angular-collection-assistant

Then check there is a reference to the script file in your header:

<script src="/bower_components/angular/angular.min.js"></script>
<script src="/bower_components/angular-collection-assistant/ng-collection-assistant.js"></script>

Finally bring in the module into your main app:

var app = angular.module("app", [
	'ngResource',
	'ng-collection-assistant'
]);

You can now use the collectionAssistant object in your controllers:

app.controller('widgetController', function($scope, collectionAssistant, Products) {
	$scope.products = Products.query({});

	$scope.$watchCollection('products', function(newProducts, oldProducts) {
		collectionAssistant(newProducts, oldProducts)
			.on('new', function(item) {
				console.log('New item discovered from server!', item);
			});
	});
}

Example use

// Somewhere inside an Angular Controller

$scope.products = [
	{
		id: 'product-foo',
		title: 'Foo',
	},
	{
		id: 'product-bar',
		title: 'Bar',
	},
	{
		id: 'product-baz',
		title: 'Baz',
	}
];

$scope.$watchCollection('products', function(newProducts, oldProducts) {
	collectionAssistant(newProducts, oldProducts)
		.indexBy('id')
		.on('new', function(item) {
			console.log('New Item!', item);
		});
		.on('deleted', function(item) {
			console.log('Deleted Item!', item);
		})
		.on('finally', $scope.redraw);
});

Enabling deep comparison also allows update detection of individual collection items:

$scope.$watchCollection('products', function(newProducts, oldProducts) {
	collectionAssistant(newProducts, oldProducts)
		.indexBy('id')
		.deepComparison()
		.on('new', function(item) {
			console.log('New Item!', item);
		});
		.on('deleted', function(item) {
			console.log('Deleted Item!', item);
		})
		.on('update', function(item, oldItem) {
			console.log('Item', item, 'has changed from', oldItem);
		});
});

Keywords

FAQs

Package last updated on 12 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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