sjs-base-model
Advanced tools
Comparing version 1.4.0 to 1.5.0
import { BaseObject } from './BaseObject'; | ||
import { IBaseModelOptions } from './IBaseModelOptions'; | ||
import { IBaseModel } from './IBaseModel'; | ||
import { IConvertOption } from './IConvertOption'; | ||
import { IConversionOption } from './IConversionOption'; | ||
export declare class BaseModel extends BaseObject implements IBaseModel { | ||
@@ -9,3 +9,3 @@ static readonly IS_BASE_MODEL: boolean; | ||
constructor(opts?: IBaseModelOptions); | ||
update(data?: any, convertOptions?: IConvertOption): any; | ||
update(data?: any, conversionOptions?: IConversionOption): any; | ||
toJSON(): any; | ||
@@ -12,0 +12,0 @@ toJSONString(): string; |
@@ -0,0 +0,0 @@ export declare enum ConversionTypeEnum { |
export { BaseModel } from './BaseModel'; | ||
export { Util } from './Util'; | ||
export { ConversionTypeEnum } from './ConversionTypeEnum'; | ||
export { IConvertOption } from './IConvertOption'; | ||
export { IConversionOption } from './IConversionOption'; |
import { ConversionTypeEnum } from './ConversionTypeEnum'; | ||
import { IConvertOption } from './IConvertOption'; | ||
import { IConversionOption } from './IConversionOption'; | ||
export declare class Util { | ||
@@ -9,4 +9,4 @@ private static _idCounter; | ||
static toBoolean(value: string | number | boolean): boolean; | ||
static convertDataUsingOptions(data: object, convertOptions: IConvertOption): void; | ||
static convertDataToType(propertyData: string | number, convertType: ConversionTypeEnum): string | number | boolean; | ||
static convertDataUsingOptions(data: object, conversionOptions: IConversionOption): void; | ||
static convertDataToType(propertyData: string | number, conversionType: ConversionTypeEnum): string | number | boolean; | ||
} |
{ | ||
"name": "sjs-base-model", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"files": [ | ||
"lib/" | ||
"lib/", | ||
"src/" | ||
], | ||
@@ -9,0 +10,0 @@ "repository": "https://github.com/codeBelt/sjs-base-model.git", |
121
README.md
@@ -25,10 +25,13 @@ # sjs-base-model | ||
``` | ||
```javascript | ||
const carModel = new CarModel(apiData); | ||
``` | ||
This is how you should extend `sjs-base-model` | ||
```javascript | ||
import {BaseModel} from 'sjs-base-model'; | ||
export class CarModel extends BaseModel { | ||
export default class CarModel extends BaseModel { | ||
@@ -41,3 +44,3 @@ make = ''; | ||
constructor(data = {}) { | ||
constructor(data) { | ||
super(); | ||
@@ -56,7 +59,9 @@ | ||
``` | ||
Model Explained | ||
```javascript | ||
import {BaseModel} from 'sjs-base-model'; | ||
export class CarModel extends BaseModel { | ||
export default class CarModel extends BaseModel { | ||
@@ -76,3 +81,3 @@ // The class properties must match the data properties being passed in. Otherwise they will be ignored | ||
constructor(data = {}) { | ||
constructor(data) { | ||
super(); | ||
@@ -94,6 +99,99 @@ | ||
## BaseModel Conversion Types | ||
`BaseModel` has the abiltiy to convert data when passed to the `update` method. For example if a string number was passed in `"2"` and you wanted to be an actual number `2` then you can give it the property name and associate it with the correct `ConversionTypeEnum`. Currently it only supports `number`, `float` and `boolean`. See below for an example: | ||
```javascript | ||
const json = { | ||
"seed": "abc", | ||
"results": "3", // We want this to be a boolean | ||
"page": "1", // We want this to be a number | ||
"version": "1.1" // We want this to be a float number | ||
}; | ||
const model = new SomeModel(json); | ||
``` | ||
JavaScript Version | ||
```javascript | ||
import {BaseModel, ConversionTypeEnum} from 'sjs-base-model'; | ||
export default class SomeModel extends BaseModel { | ||
seed = ''; | ||
results = false; // Was a string but converted into a boolean by IConversionOption | ||
page = null; // Was a string but converted into a number by IConversionOption | ||
version = null; // Was a string but converted into a float by IConversionOption | ||
constructor(data) { | ||
super(); | ||
this.update(data); | ||
} | ||
update(data) { | ||
const conversionOptions = { | ||
results: ConversionTypeEnum.Boolean, | ||
page: ConversionTypeEnum.Number, | ||
version: ConversionTypeEnum.Float, | ||
}; | ||
super.update(data, conversionOptions); | ||
} | ||
} | ||
``` | ||
TypeScript Version | ||
```javascript | ||
import {BaseModel, ConversionTypeEnum, IConversionOption} from 'sjs-base-model'; | ||
export default class SomeModel extends BaseModel { | ||
public seed: string = ''; | ||
public results: boolean = false; // Was a string but converted into a boolean by IConversionOption | ||
public page: number = null; // Was a string but converted into a number by IConversionOption | ||
public version: number = null; // Was a string but converted into a float by IConversionOption | ||
constructor(data: Partial<SomeModel>) { | ||
super(); | ||
this.update(data); | ||
} | ||
public update(data: Partial<SomeModel>): void { | ||
const conversionOptions: IConversionOption = { | ||
results: ConversionTypeEnum.Boolean, | ||
page: ConversionTypeEnum.Number, | ||
version: ConversionTypeEnum.Float, | ||
}; | ||
super.update(data, conversionOptions); | ||
} | ||
} | ||
``` | ||
## BaseModel Properties | ||
There are a couple of properties on the `BaseModel`. If you call the `.toJSON();` method on the model it will remove all `sjs-base-model` specific properties. | ||
#### sjsId | ||
Each `sjs-base-model` that is created has a unique model id. You can access it by the `sjsId` property. | ||
```javascript | ||
const carModel = new CarModel(); | ||
carModel.sjsId; // unique model id | ||
``` | ||
#### sjsOptions | ||
TODO | ||
## BaseModel Methods | ||
#### update(json) | ||
Example how to use the `update` method which will only change the property value(s) that were passed in | ||
Example how to use the `update` method which will only change the property value(s) that were passed in. | ||
```javascript | ||
@@ -105,2 +203,3 @@ carModel.update({year: 2015, feature: {abs: true}}); | ||
Converts the BaseModel data into a JSON object and deletes the sjsId property. | ||
```javascript | ||
@@ -112,2 +211,3 @@ const json = carModel.toJSON(); | ||
Converts a BaseModel to a JSON string, | ||
```javascript | ||
@@ -119,2 +219,3 @@ const jsonStr = carModel.toJSONString(); | ||
Converts the string json data into an Object and calls the update method with the converted Object. | ||
```javascript | ||
@@ -128,2 +229,3 @@ const str = '{"make":"Tesla","model":"Model S","year":2014}' | ||
Creates a clone/copy of the BaseModel. | ||
```javascript | ||
@@ -135,6 +237,7 @@ const clone = carModel.clone(); | ||
You will need to do `as any` when assigning the function model to the type of model so the compiler doesn't complain. Notice `FeatureModel as any;` and `[ColorModel as any];` | ||
```typescript | ||
import {BaseModel} from 'sjs-base-model'; | ||
export class CarModel extends BaseModel { | ||
export default class CarModel extends BaseModel { | ||
@@ -149,3 +252,3 @@ make: string = ''; | ||
constructor(data: any = {}) { | ||
constructor(data: Partial<CarModel>) { | ||
super(); | ||
@@ -156,3 +259,3 @@ | ||
update(data: any): void { | ||
update(data: Partial<CarModel>): void { | ||
super.update(data); | ||
@@ -176,2 +279,4 @@ | ||
* 2018-05-09 v1.5.0 Rename IConvertOption to IConversionOption to match with ConversionTypeEnum | ||
* 2018-04-20 v1.4.0 Add the ability to convert property values to ConversionTypeEnum.Float, ConversionTypeEnum.Number or ConversionTypeEnum.Boolean with IConvertOption. | ||
@@ -178,0 +283,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
63136
21
648
282
1