npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modellist

v0.4.6

Published

ModelList

Downloads

17

Readme

angular-modellist

Build Status NPM version Coverage Status

Why?

When storing model data in a service, you most likely will need to bind to it in your view(s). If we bind this data in multiple places we run the risk of loosing the instance if we aren't careful.

var myCtrl1 = function(myService) {
	$scope.arrayData = myService.data;
};

var myCtrl2 = function(myService) {
	$scope.arrayData = myService.data;

	$scope.getNewData = function() {
		$http.get("someUrl").success(res) {
			$scope.arrayData = res; // This causes us to loose our reference
		};
	};
};

This service provides an object that wraps an array, but always keeps the same instance when operations are performed on it.

// myService.data = new ModelList();

var myCtrl1 = function(myService) {
	$scope.arrayData = myService.data;
};

var myCtrl2 = function(myService) {
	$scope.arrayData = myService.data;

	$scope.getNewData = function() {
		$http.get("someUrl").success(res) {
			$scope.arrayData.overwrite(res); // Now our other controls arrayData updates with this one!
		};
	};
};

How To Use:

####AngularJS####

Just include the module into your app:

angular.module("myApp", ["ModuleList"]);

Inject:

angular.module("myApp", ["ModelList"]).factory("myService", function(ModelList, $http) {
	
	var myService = new ModelList();

	myService.update = function() {
		$http.get("myUrl").success(function(res) {
			myService.overwrite(res.data);
		});
	};
	
	return myService;
})
.controller("myCtrl", function($scope, myService) {
	$scope.myService = myService;
})
.controller("myOtherCtrl", function($scope, myService) {
	$scope.myService = myService;
});

Use the getBindableList method to bind to directives.

<div ng-controller="myCtrl">
	<div ng-repeat="item in myService.getBindableList()">
	</div>
	<button ng-click="myService.update()">
</div>
<div ng-controller="myOtherCtrl">
	<div ng-repeat="item in myService.getBindableList()">
	<button ng-click="myService.update()">
</div>

Both of the ng-repeat directives will be in sync when the list is updated.

####NodeJS####

var ModelLIst = require("modellist");

var list = new ModelList();

####Browser#### The ModelList class will be available on the window object

var list = new ModelList();

API

####Native methods:#### Most array methods are supported in their native form except a couple:

  • join
  • pop
  • push
  • reverse
  • shift
  • unshift
  • splice
  • sort
  • forEach: Uses a minimal implementation if not supported
  • some
  • every
  • indexOf: Uses a minimal implementation if not supported
  • lastIndexOf
  • reduce
  • reduceRight
  • map: (Chainable) Mutates the array and does not return a new array
  • concat: (Chainable) Mutates the array and does not return a new array
  • filter: (Chainable) Mutates the array and does not return a new array
  • slice: (Chainable) Mutates the array and does not return a new array

If your browser doesn't support the method natively, it won't be available.

####Custom methods:#####

  • get(Number:index): Gets an item at index
  • set(*, Number:index): (Chainable) Sets an item at index
  • clean(): (Chainable) Empties the array
  • overwrite(Array:array): (Chainable) Overwrites the array with the items in the new array
  • clone(): Returns a new ModelList object with a cloned list
  • getBindableList(): Returns the array. Only should be used for binding
  • pull(*): (Chainable) Removes object instances from the array
  • merge(Array:list, [Object:options]): (Chainable) Merges an array of objects with the list of objects. Useful for merging API responses while keeping the same object references.
    • [Function|String:options.comparator]: The comparator is used for matching 2 objects together. It can be an ID key or a custom compare function.
    • [Function:options.merger]): An optional merger function can be used to perform the merge. If omitted a default object extend will be performed.
    • [Function:options.accumulator]: An optional function to handle non matched elements. If omitted it will perform a splice of the element at it's position. If you transform the value in this function you must return it so the ModelList can keep track of it.
    • [Function:options.remover]: An optional function to handle elements in the list but not in the response. If omitted it will be a noop function.

A length property is also kept in sync just like native array behaviour.

####Static methods:####

  • create(Array:array, [Boolean:clone]) Returns a new instance of ModelList
  • convert(Object:object, [Boolean:deep]) Converts all array instances into ModelList instances