js-model
Data model for javascript
中文文档
Install
npm install
npm install js-model --save
Model
Column Define:
- String: "" || String
- Number: 0 || Number
- Date: Date
- Array: []
- Object: {}
Default Parameter
// when use dispose data, remove empty array. removeEmptyArray: false // when use parse data, remove null value. removeNull: false // remove null value from array. removeNullFromArray: false // remove null value from object. removeEmptyObject: true
Const
ModelS // money ten 十 ModelB // money hundred 百 ModelQ // money thousand 千 ModelW //money ten thousand 万 ModelSW // money one hundred thousand 十万 ModelBW // money million 百万 ModelQW // money ten million 千万 ModelY // money billion 亿
Methods
-
parse:
- Fill property: Creating a complete object data, allows you to get rid of the boredom of {{a&&a.b?a.b.c:''}}
- Data conversion: Data standardization conversion, when data is transferred from the background, the date is a timestamp, the amount is in unit, parse method is to help you convert time stamp to time string, the amount is converted in a certain unit, and also can help you to complete all the fields.
- Default value: define default value
-
dispose:
- When you need to transfer the data to the background, convert the date into a timestamp, convert the amount to the amount in the unit, standardize the data format, and delete the empty data.
Example: the value modified by input is String, and is converted to digital format through dispose.
Basic
Basic.js
;let Basic =id: 0source:type: Dateformat: 'l' // use manba date format, "l": "YYYY-MM-DD",description: ""tags: 0companyId: ""rate:type: Numberdefault: 08 // use default value, only effective for String, Number, Datesalary:type: Numberunit: ModelQ // money transfor, a unit of 1000;;
parse
Usage 1: fill property
let basicValue = Basic;
basicValue:
id: nullsource: nulldescription: nulltags:companyId: nullrate: 08 // use default valuesalary: null
Usage 2: conversion amount and date
let basicValue = Basic;
basicValue:
id: nullsource: "2017-06-09" //description: nulltags:companyId: nullrate: 01salary: 10 //10000 conversion to a thousand units
dispose
Usage 1: remove null property
let basicValue = Basic;
basicValue: Consistent with the values converted from parse
source: "2017-06-09T00:00:00+08:00"salary: 10000rate: 01
Advanced
// Basic.js let Basic = id: 0 companyId: "" rate: 0;; // Edu.js let Edu = id: 0 major: "" school: "";; // User.js ;;let User = basic: Basic edu: Edu;;
parse
let user = User
result:
basic: id: 123123 companyId: null rate: null edu: id: 12 school: null major: null
dispose
let user = User
result:
basic: id:123123 companyId: 123 edu: id: 12 school: "school"
Extend
Single display and dispose
const info =salary:type: Number{return datasalary / 1000}{return datasalary * 1000};info// {salary: 10}info// {salary: 20000}
Extend Model
{let b = super;ifbimgUrlstypelength == 0bimgUrlstypereturn b;}{return super}const info =imgUrls:type: '';info
result:
imgUrls: type: 'http://*****'
Config
manba-config.js The default is the ISO date format of the current time zone, for example: 2016-04-19T00:00:00+08:00
;// Redefining the format of the date conversionModel