vvpvvp / model Goto Github PK
View Code? Open in Web Editor NEWData model for javascript
License: MIT License
Data model for javascript
License: MIT License
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-core": "^6.26.0",
"chai": "^4.1.2",
"manba": "^1.0.1"
const __Model = require('../src/model');
let _dataM = new __Model({
a: Number,
b: String,
c: Date,
d: Boolean,
e: Array,
f: Object
});
let _r = _dataM.parse({
a: 'sdf12asd',
b: 12
});
parse 数据可以是这样的 吗
{ c: new Date, d: false, e: [], f: {}, a: 0, b: '12' }
可以支持typescript吗
import Model from 'js-model'
const modelTest = new Model({
a: {
type: Array,
defalut: []
}
})
console.log(modelTest.parse({}))
返回结果
{
a: {
type: null,
defalut: Array(0)
}
}
正常不应该是返回
{
a: []
}
吗?
我直接集成到vue中就是不行
觉得挺不错的, 但是这个homepage改一下吧, npm右边上点一下是moment, 大写的尴尬
demo: https://codepen.io/anon/pen/qyXjyj
模型定义:
let User = new Model({
id: 0,
follower_num: 0,
following_num: 0,
is_following: false,
type: String,
real_name: String,
avatar_url: String,
title: String,
introduction: String,
})
我发现把 type: String
改成 type1: String
就没问题了,问题可能出现在对属性名的判断上?
另外,请问能否支持Boolean类型的定义?默认false。
就是模型定义里面的 key 是 name, 而 服务端返回的数据里面就不按约定叫 name, 他给改成了 username, 而我们事先已经按照约定写了N多的业务逻辑,这时候去改模型定义的 name 显然是不合适的,但如果可以定义一个转义 key, 例如:
export default new Model({
int: Number,
date: {
type: Date,
format: 'YYYY-MM-DD' //此处使用manba日期工具
},
money: {
type: Number,
unit: Model.W
},
name: {
type: String,
jsonKey: ‘username’
}
})
然后可以按照转义 key username 去取值,这样就不需要到各种业务里面去把 name 改为 username 了,只需要在模型定义处定义一下就可以了
不知道我是否说清楚了
const banners = new Model({
imgUrls: {
type: [''],
default: ['http://via.placeholder.com/750x306'],
},
});
这样用的话会返回一个空数组
const target = [{a: 1}, {a: 2}]
,
How to define target as a Model, then I can use target.parse() to parse
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.