🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

angular-collection-assistant

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
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created

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);
		});
});

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