Coder Social home page Coder Social logo

angular-collection's Introduction

AngularCollection

js-standard-style npm version Bower version npm downloads License

Build Status Code Climate Test Coverage Dependency Status devDependency Status

angular-collection.js

Description

AngularCollection is a collection module for AngularJS.

Installation

angular-collection is a bower component you should be able to install it by running:

bower install angular-collection

or if you already have a bower based project you can add angular-collection to your dependency list in bower.json

 "dependencies": {
    ...
    "angular-collection": "0.x.x"
    ...
  }

API

  • add(obj, options)

  • addAll(array, options)

  • sort()

  • get(obj | id)

  • update(obj)

  • remove(obj)

  • removeAll()

  • last()

  • at(index)

  • size()

  • all()

Usage

Specify dependencies

var app = angular.module('myApp', ['ngCollection']);

Define new factory

app.factory("TodoCollection", function($collection){
	var TodoCollection = $collection;

	return TodoCollection;
})

Get collection instance

var todos = TodoCollection.getInstance();

Add new records

_id property will be generated and attached to each new record.

todos.add({ title: "todo1" });
todos.add({ title: "todo2" });
todos.add({ title: "todo0" }, {index: 0});

Get a single record

Get a record from the collection, specified by an id or by passing in a record.

	var todo = todos.get(10);

Update a single record

If a record is already in the collection, its attributes will be merged.

	todos.update({ id: 1, title: 'todos3' });

Remove a record from the collection

	todos.remove({ id: 1, title: 'todos3' });

Get a single record, spcified by index

	todos.at(1);

Sort records as they are added to collection

Sort the collection by title descending each time you call add()

	var todos = $collection.getInstance({comparator: "-title"});
	todos.add({ title: "todo1" });		// performs sort
	todos.add({ title: "todo2" });		// performs sort

Sort records on demand

Sorts the collection by title descending but only when sort() is called

	var todos = $collection.getInstance();
	todos.add({ title: "todo1" });
	todos.add({ title: "todo2" });
	todos.sort('-title');			// performs sort

Options

You can pass a single parameter to getInstance to specify additional options.

var todos = TodoCollection.getInstance(options);

Currently the only options available are idAttribute and comparator.

var todos = TodoCollection.getInstance({idAttribute: 'id', comparator: '-created_at'});

Contributors:

License:

The MIT License

angular-collection's People

Contributors

0x-r4bbit avatar ajoslin avatar atefbb avatar damrbaby avatar failpunk avatar falsetto avatar jseppi avatar mkuklis avatar tomkuk avatar warnerandy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-collection's Issues

.where(attributes)

Is it possible to make a similar functional http://backbonejs.org/#Collection-where ?
wherecollection.where(attributes)
Return an array of all the models in a collection that match the passed attributes. Useful for simple cases of filter.

var friends = new Backbone.Collection([
{name: "Athos", job: "Musketeer"},
{name: "Porthos", job: "Musketeer"},
{name: "Aramis", job: "Musketeer"},
{name: "d'Artagnan", job: "Guard"},
]);

var musketeers = friends.where({job: "Musketeer"});

.sort()

Please show an example of how this function works

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.