1.0.35 • Published 6 years ago

fusionjs v1.0.35

Weekly downloads
16
License
MIT
Repository
github
Last release
6 years ago

Fusion JS

"One library to bind them all."

Fusion JS provides:

  • Data modelling and state management utilities.
  • Work in progress;
    • A workspace boilerplate that enables you to integrate your preferred libraries with ease.
    • A library of accessible UI components (Planned for future release)

FusionJS is agnostic with regards to the JS framework or library you want to use. And was infact created specifically to enable apps to be written with any library without restriction to include swithcing app development to a different set of frameworks and libraries if required.

Data modelling and state management utilities

The data modelling and state management utilities consists of two parts; FusionModel and FusionImmutable.

FusionModel

FusionModel is the M in MVC and provides a means to represent data that your application manages. It provides one-to-one and one-to-many relationship capabilities

Getting started

npm install fusionjs

Then require it into any module.

import {FusionModel} from 'fusionjs';

FusionModel is designed as an Abstract class and is not expected to be instantiated. To use it, extend it to create your model class. Derived classes must contain constructor functions and must call init() after super() as shown. Fields property must be supplied.

import {FusionModel} from 'fusionjs';
export class TestModel extends FusionModel {

	idProperty = 'testModelId';

	fields = [{
		name: 'testModelId',
		type: 'string'
	}];

	constructor(data) {
		super(data);
		this.init();
	}
}

Nested structures

To represent one-to-one and one-to-many relationships, apply hasMany and hasOne properties as shown;

/*
For example, for the following sample data;
let sampleData = { 
	testId: 123,
	testValue: 'one, two, three',
	relOne: {
		testRelId: 1123,
		testRelValue: 'two hundred and something'
	},
	rels: [{
		testRelId: 2123,
		testRelValue: 'two hundred and something odd'
	}, {
		testRelId: 2122,
		testRelValue: 'two hundred and something even'
	}]
}
*/

import {FusionModel} from 'fusionjs';
export class TestRelModel extends FusionModel {

	idProperty = 'testRelId';

	fields = [{
		name: 'testRelId',
		type: 'string'
	}, {
		name: 'testRelValue',
		type: 'string'
	}];

	constructor(data) {
		super(data);
		this.init();
	}
}
export class TestModel extends FusionModel {

	idProperty = 'testId';

	fields = [{
		name: 'testId',
		type: 'string'
	}, {
		name: 'testValue',
		type: 'string'
	}];

	hasOne = [{
        name: 'rel',
        model: TestRelModel
    }];

	hasMany = [{
        name: 'rels',
        model: TestRelModel
    }];

	constructor(data) {
		super(data);
		this.init();
	}
}

Using your model

let testModel = new TestModel(sampleData);
//get model data
testModel.get(); //Will return the top level data 
test.rels();	//returns a store contaning your hasMany data collection
test.rels().get(); //returns a collection (an array) of records representing your "hasMany" data
test.relOne(); //returns a record representing your "hasOne" data

//you can set data after instantiation like so;
let testModel = new TestModel();
testModel.set(sampleData);

//you can set data to a deeply nested record like so;
testModel.rels().get()[0].set({
	testRelId: 45678,
	testRelValue: 'test'
});

//return a JS object of data in your model
testModel.toObject();

//checking equality
testModel.equals(anotherTestModel); //(Value equality checker) will return true if both models contain the same values (check does NOT include nested data)
testModel.deepEquals(anotherTestModel); //(Value equality checker) will return true if both models contain the same values (check includes nested data)
testModel.strictEquals(anotherTestModel); //(Value/Reference equality checker)  will return true if both models contain the same data (check includes nested data) and are the same instance

//To find a deeply nested record
testModel.find(myDeeplyNestedRecord);

FusionImmutable

FusionJS provides immutable state management capability via its FusionImmutable module.

Usage

Require it into any module.

import {FusionImmutable} from 'fusionjs';
//Where TestModel is a derived FusionModel class
let fusionImmutable = new FusionImmutable(TestModel),
	initialState = fusionImmutable.fromJS({
		id: null,
		rel: {},
		rels: []
	});

To update with state immutability, use the merge() method

import {FusionImmutable} from 'fusionjs';
//Where TestModel is a derived FusionModel class
let fusionImmutable = new FusionImmutable(TestModel),
	state = fusionImmutable.fromJS({
		id: null,
		rel: {},
		rels: []
	});
fusionImmutable.merge(state, newData);
//Or if data is meant to update a deeply nested record within the model, then pass the record as a third argument;
fusionImmutable.merge(state, newData, record);

Known Issues

  • TypeError: Class constructors FusionModel cannot be invoked without 'new'

    • Solution: You will need to use a babel preset that is targeted to your platform

      		```javascript
      		 npm install babel-preset-es2015-node-auto // (OR specify one, e.g. babel-preset-es2015-node5, babel-preset-es2015-node6)
      
      		 //then configure your loader, for example in webpack config;
      		 {
      			test: /\.js?$/,
      			exclude: /node_modules/,
      			loader: 'babel',
      			query: {
      				presets: ['react', 'es2015-node6', 'stage-0']
      			}
      		}
      		 ```
1.0.35

6 years ago

1.0.34

7 years ago

1.0.33

7 years ago

1.0.32

7 years ago

1.0.31

7 years ago

1.0.30

7 years ago

1.0.29

7 years ago

1.0.28

7 years ago

1.0.27

7 years ago

1.0.26-beta-1.22

7 years ago

1.0.26-beta-1.21

7 years ago

1.0.26-beta-1.20

7 years ago

1.0.26-beta-1.19

7 years ago

1.0.26-beta-1.18

7 years ago

1.0.26-beta-1.17

7 years ago

1.0.26-beta-1.16

7 years ago

1.0.26-beta-1.15

7 years ago

1.0.26-beta-1.14

7 years ago

1.0.26-beta-1.13

7 years ago

1.0.26-beta-1.12

7 years ago

1.0.26-beta-1.10

7 years ago

1.0.26-beta-1.9

7 years ago

1.0.26-beta-1.8

7 years ago

1.0.26-beta-1.7

7 years ago

1.0.26-beta-1.6

7 years ago

1.0.26-beta-1.5

7 years ago

1.0.26-beta-1.4

7 years ago

1.0.26-beta-1.3

7 years ago

1.0.26-beta-1.2

7 years ago

1.0.26-beta-1.1

7 years ago

1.0.26-beta-1.0

7 years ago

1.0.25

7 years ago

1.0.24

7 years ago

1.0.22

7 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.23

7 years ago

1.0.21

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago