Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zation/traction

An util tool to merge store data for flux architecture
https://github.com/zation/traction

Last synced: about 7 hours ago
JSON representation

An util tool to merge store data for flux architecture

Awesome Lists containing this project

README

        

# traction

Help flux store to merge new data trigger from action, for example: API, user interface, local storage...
This tool will make your store management super easy by one line code: `traction.merge(newData).to(array).basedOn('id');`

## Install

Install with [npm](https://www.npmjs.com/package/traction)

```bash
npm install --save traction
```

## Examples

### Merge object to array

Say we have an UsersStore, and we fetch one user from API:

```js
var traction = require('traction');
var users = [{
id: 1,
name: 'a'
}];

var onFetchOneUserSuccess = function(newUser) {
users = traction.merge(newUser).to(users).basedOn('id');
};

onFetchOneUserSuccess({
id: 1,
name: 'b'
});
onFetchOneUserSuccess({
id: 2,
name: 'c'
});
console.log(users);
```

The output should be:

```js
[{
id: 1,
name: 'b'
}, {
id: 2,
name: 'c'
}]
```

### Merge array to array

Say we have an UsersStore, and we fetch multiple users from API:

```js
var traction = require('traction');
var users = [{
id: 1,
name: 'a'
}, {
id: 2,
name: 'b'
}];

var onFetchOneUserSuccess = function(newUsers) {
users = traction.merge(newUsers).to(users).basedOn('id');
};

onFetchOneUserSuccess({
id: 2,
name: 'c'
}, {
id: 3,
name: 'd'
});
console.log(users);
```

The output should be:

```js
[{
id: 1,
name: 'a'
}, {
id: 2,
name: 'c'
}, {
id: 3,
name: 'd'
}]
```

**NOTICE**: the merge is deeply merge.

### Merge based on multiple keys

Say we have an UsersStore, and we fetch one user from API:

```js
var traction = require('traction');
var users = [{
name: 'a',
gender: 'male',
age: 11
}, {
name: 'a',
gender: 'female',
age: 20
}];

var onFetchOneUserSuccess = function(newUser) {
users = traction.merge(newUser).to(users).basedOn('name', 'gender');
};

onFetchOneUserSuccess({
name: 'a',
gender: 'female',
age: 30
});

console.log(users);
```

The output should be:

```js
[{
name: 'a',
gender: 'male',
age: 11
}, {
name: 'a',
gender: 'female',
age: 30
}]
```