GithubHelp home page GithubHelp logo

daedalus28 / underscoreko Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kamranayub/underscoreko

0.0 1.0 0.0 401 KB

A tiny library that adds all of the Underscore.js collection/array methods to Knockout observable arrays

License: MIT License

JavaScript 100.00%

underscoreko's Introduction

UnderscoreKO

Underscore.js + Knockout = Array Happy Fun Time!

This tiny library (< 1KB) adds all the collection and array methods you've come to love in Underscore.js to your Knockout observable arrays. It will not override any existing functionality (if any exists).

Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)

Install

Manual

Download .\build\underscore-ko-{version}.min.js and put it in your project.

Nuget

Install the UnderscoreKO Nuget package.

PM> Install-Package UnderscoreKO

Usage

Use the Underscore methods just as you normally would, but now you don't need to specify the array to use:

var vm = {
    arr: ko.observableArray([1, 2, 3]);
};

vm.arr.each(function (x) {
    // do something
});

// This returns a raw array, not a ko.observableArray
var newArr = vm.arr.union([0, 1]);

In addition, there are several functions you can use that will mutate (change) the underlying array, which are provided as convenient shortcuts.

// Re-structure the observable array [1, 2, 3]
vm.arr.without_(2);
// vm.arr() is now equal to [1, 3]

// Without this, you would need to do:
vm.arr(vm.arr.without(2));

Live Demo

View the live jsFiddle demo

Documentation

See the Underscore.js documentation for more information on the API. All array and collection methods are supported with the exception of any I felt didn't provide value (.toArray() for example).

See spec.js for examples of how to use specific functions, but I'm telling you, it's as you'd expect.

Mutator Methods

  • filter_, select_
  • reject_
  • invoke_
  • sortBy_
  • groupBy_
  • shuffle_
  • rest_, tail_
  • compact_
  • flatten_
  • without_
  • union_
  • intersection_
  • difference_
  • uniq_, unique_
  • zip_

Contributing

See Contributing

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.