使用数据模型

为javascript准备的数据模型工具。

所有的数据模型将放在 src/js/model文件夹中。

安装

npm安装

  1. npm install js-model --save

Model

字段定义:

  • String: “” || String
  • Number: 0 || Number
  • Date: Date
  • Array: []
  • Object: {}

Default Parameter

  1. {
  2. //dispose的时候移除空数组
  3. removeEmptyArray: false,
  4. //parse的时候移除null数据
  5. removeNull: false,
  6. //移除null数据从数组中
  7. removeNullFromArray: false,
  8. //从子对象中移除空对象
  9. removeEmptyObject: true,
  10. }

Const

  1. Model.S // money ten 十
  2. Model.B // money hundred 百
  3. Model.Q // money thousand 千
  4. Model.W //money ten thousand 万
  5. Model.SW // money one hundred thousand 十万
  6. Model.BW // money million 百万
  7. Model.QW // money ten million 千万
  8. Model.Y // money billion 亿

方法

parse

  • 创建完整对象数据,让你摆脱{{a&&a.b?a.b.c:’’}}这种无聊的判断了
  • 数据标准化转换,当数据从后台传输过来的时候,日期是时间戳,金额是以元为单位,parse方法是帮你转换时间戳至时间字符串,金额以一定单位转换好,并且可以帮助你补全好所有的字段。

dispose

当你需要把数据传送至后台之前,把日期转换成时间戳,把金额转换为以元为单位的数额,标准化数据格式,删除为空的数据。例:通过input修改的数值为String, 通过dispose转换成数字格式。

基本

Basic.js

  1. import Model from "js-model";
  2. let Basic = new Model({
  3. id: 0,
  4. source: {
  5. type: Date,
  6. format: 'l' // 使用manba日期格式化, "l": "YYYY-MM-DD",
  7. },
  8. description: "",
  9. tags: [ 0 ],
  10. companyId: "",
  11. rate: {
  12. type: Number,
  13. default: 0.8 // 使用默认值,只对 String, Number, Date 类型的值有效。
  14. },
  15. salary: {
  16. type: Number,
  17. unit: Model.Q // 金额转换,此处单位为 千
  18. }
  19. });
  20. export default Basic;

parse

Usage 1: 补充字段

  1. import Basic from './Basic.js'
  2. let basicValue = Basic.parse({});

basicValue:

  1. {
  2. id: null,
  3. source: null,
  4. description: null,
  5. tags: [],
  6. companyId: null,
  7. rate: 0.8, // use default value
  8. salary: null
  9. }

Usage 2: 转换金额与日期

  1. import Basic from './Basic.js'
  2. let basicValue = Basic.parse({
  3. source: "2017-06-09T00:00:00+08:00",
  4. salary: 10000,
  5. rate: 0.1
  6. });

result:

  1. {
  2. id: null,
  3. source: "2017-06-09", //
  4. description: null,
  5. tags: [],
  6. companyId: null,
  7. rate: 0.1,
  8. salary: 10 //10000 conversion to a thousand units
  9. }

dispose

Usage 1: 删除null值的属性,并转换金额与日期

  1. import Basic from './Basic.js'
  2. let basicValue = Basic.dispose({
  3. id: null,
  4. source: "2017-06-09",
  5. description: null,
  6. tags: [],
  7. companyId: null,
  8. rate: "0.1",
  9. salary: 10
  10. });

result: 与从parse的值一致

  1. {
  2. source: "2017-06-09T00:00:00+08:00",
  3. salary: 10000,
  4. rate: 0.1
  5. }

进阶

  1. // Basic.js
  2. let Basic = new Model({
  3. id: 0,
  4. companyId: "",
  5. rate: 0
  6. });
  7. export default Basic;
  8. // Edu.js
  9. let Edu = new Model({
  10. id: 0,
  11. major: "",
  12. school: ""
  13. });
  14. export default Edu;
  15. // User.js
  16. import Edu from "./Edu";
  17. import Basic from "./Basic";
  18. let User = new Model({
  19. basic: Basic,
  20. edu: [Edu]
  21. });
  22. export default User;

parse

  1. import User from './User'
  2. let user = User.parse({
  3. basic:{
  4. id:123123
  5. },
  6. edu:[{
  7. id: 12
  8. }],
  9. })

result:

  1. {
  2. basic: {
  3. id: 123123,
  4. companyId: null,
  5. rate: null
  6. },
  7. edu: [{
  8. id: 12,
  9. school: null
  10. major: null,
  11. }]
  12. }

dispose

  1. import User from './User'
  2. let user = User.dispose({
  3. basic:{
  4. id:123123,
  5. companyId: 123,
  6. rate: null
  7. },
  8. edu:[{
  9. id: 12,
  10. school: "school"
  11. major: null,
  12. }],
  13. })

result:

  1. {
  2. basic: {
  3. id:123123,
  4. companyId: 123,
  5. },
  6. edu: [{
  7. id: 12,
  8. school: "school"
  9. }]
  10. }

扩展

单独编写display和dispose

  1. const info = new InfoModel({
  2. salary: {
  3. type: Number,
  4. parse(data) {
  5. return data.salary / 1000
  6. },
  7. dispose(data) {
  8. return data.salary * 1000
  9. }
  10. },
  11. });
  12. info.parse({salary: 10000})
  13. // {salary: 10}
  14. info.parse({salary: 20})
  15. // {salary: 20000}

继承Model

  1. class InfoModel extends Model {
  2. parse(data) {
  3. let b = super.parse(data);
  4. if(b.imgUrls.type.length == 0) {
  5. b.imgUrls.type.push('http://*****')
  6. }
  7. return b;
  8. }
  9. dispose(data, param) {
  10. return super.dispose(data, param)
  11. }
  12. }
  13. const info = new InfoModel({
  14. imgUrls: {
  15. type: ['']
  16. },
  17. });
  18. info.parse({})

result:

  1. {
  2. imgUrls: {
  3. type: ['http://*****']
  4. },
  5. }

配置

model-config.js默认的日期转换是使用当前时区的ISO日期格式, 比如: 2016-04-19T00:00:00+08:00

  1. import Model from 'js-model';
  2. // Redefining the format of the date conversion
  3. Model.config({
  4. disposeDateFormat(date) {
  5. // change to use timestamp
  6. return manba(date).time();
  7. }
  8. })