Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/zation/traction
- Owner: zation
- License: mit
- Created: 2015-04-08T05:35:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-05-25T13:18:07.000Z (over 9 years ago)
- Last Synced: 2024-04-24T21:43:56.369Z (7 months ago)
- Language: JavaScript
- Size: 152 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
}]
```