observ-array

WebJar for observ-array

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

observ-array
Last Version

Last Version

3.2.1
Release Date

Release Date

Type

Type

jar
Description

Description

observ-array
WebJar for observ-array
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/Raynos/observ-array

Download observ-array

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/observ-array/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>observ-array</artifactId>
    <version>3.2.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/observ-array/
implementation 'org.webjars.npm:observ-array:3.2.1'
// https://jarcasting.com/artifacts/org.webjars.npm/observ-array/
implementation ("org.webjars.npm:observ-array:3.2.1")
'org.webjars.npm:observ-array:jar:3.2.1'
<dependency org="org.webjars.npm" name="observ-array" rev="3.2.1">
  <artifact name="observ-array" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='observ-array', version='3.2.1')
)
libraryDependencies += "org.webjars.npm" % "observ-array" % "3.2.1"
[org.webjars.npm/observ-array "3.2.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.webjars.npm » adiff jar [0.2.12,0.3)
org.webjars.npm : observ jar [0.2.0,0.3)
org.webjars.npm : xtend jar [3.0.0,4)

Project Modules

There are no modules declared in this project.

observ-array

An array containing observable values

Example

An ObservArray is an observable version of an array, every mutation of the array or mutation of an observable element in the array will cause the ObservArray to emit a new changed plain javascript array.

var ObservArray = require("observ-array")
var ObservStruct = require("observ-struct")
var Observ = require("observ")
var uuid = require("uuid")

function createTodo(title) {
  return ObservStruct({
    id: uuid(),
    title: Observ(title || ""),
    completed: Observ(false)
  })
}

var state = ObservStruct({
  todos: ObservArray([
    createTodo("some todo"),
    createTodo("some other todo")
  ])
})

state(function (currState) {
  // currState.todos is a plain javascript todo
  // currState.todos[0] is a plain javascript value
  currState.todos.forEach(function (todo, index) {
    console.log("todo", todo.title, index)
  })
})

state.todos.get(0).title.set("some new title")
state.todos.push(createTodo("another todo"))

Transactions

Batch changes together with transactions.

var array = ObservArray([ Observ("foo"), Observ("bar") ])

var removeListener = array(handleChange)

array.transaction(function(rawList) {
  rawList.push(Observ("foobar"))
  rawList.splice(1, 1, Observ("baz"), Observ("bazbar"))
  rawList.unshift(Observ("foobaz"))
  rawList[6] = Observ("foobarbaz")
})

function handleChange(value) {
  // this will only be called once
  // changes are batched into a single diff
  value._diff //= [ [1,1,"baz","bazbar","foobar", , "foobarbaz"],
              //    [0,0,"foobaz"] ]
}

Installation

npm install observ-array

Contributors

MIT Licenced

Versions

Version
3.2.1