项目作者: chuckWu1989

项目描述 :
Better model, Better life!
高级语言: JavaScript
项目地址: git://github.com/chuckWu1989/model-decorator.git
创建时间: 2018-03-06T07:28:37Z
项目社区:https://github.com/chuckWu1989/model-decorator

开源协议:MIT License

下载


model-decorator



npm version
dependencies Status
devDependencies Status
download
License


Better model, better life

The major mission of model-decorator is to solve the data validation problem. Validating input data from user has always been a challenging task for web developer. One way to achieve this goal is to write validation codes in business logic. However, it would make your codes more complex and hard to maintain. Another solution is to use some components which already implemented validation mechanism. This way would loss some elasticity (For example, styles, features limitation etc).

model-decorator offers a lightweight method to solve this problem. All you need to do is to write viewmodel with validation decorators. Besides, you have largely elasticity to decide how to show your error message and components.

Installation

model-decorator is available from npm:

npm install model-decorator --save

Usage

Here is a quick example to get you started, it’s all you need:

View model construction

  1. import { DefineProps, Type, DisplayName, StringLen, BaseModel } from 'model-decorator';
  2. class SimpleModel extends BaseModel {
  3. @DefineProps()
  4. @DisplayName('User Name: ')
  5. @Type(PropTypes.string, { errorMessage: 'Error type of name. It should be string' })
  6. @StringLen([, 5], { errorMessage: 'Exceed max length 5 of name' })
  7. name
  8. }
  9. export default SimpleModel;

In your component

  1. import { ViewModel } from 'model-decorator';
  2. @ViewModel(SimpleModel)
  3. class Answer extends Component {
  4. render() {
  5. const { name } = this.props.model;
  6. return (
  7. <div>
  8. <div>
  9. <label htmlFor="name">
  10. <span>{name.title}</span>
  11. <input name="name" value={name.val} readOnly />
  12. <span>{name.err}</span>
  13. </label>
  14. </div>
  15. </div>
  16. );
  17. }
  18. }

That’s all!

Examples

You can find more examples in our demo pages: Click Me

Furthermore, there is a codepen playground. You can try anything if you want: Click Me

Setting of ES7 Decorator

Because decorator is a modern feature in ES7, the browser compatibility is still not so powerful. Therefore, we need to turn to babel. In Babel 7, the decorator feature is the default plugin in stage-0. You will need to include it as:

.babelrc

  1. {
  2. "presets": [
  3. "env",
  4. "stage-0"
  5. ]
  6. }

Besides, we also need plugin transform-decorators-legacy to transfer @ syntax (If you are wonder why it is legacy, you can check its repo and there is an explaination: Click Me)

  1. {
  2. "presets": [
  3. "env",
  4. "stage-0"
  5. ],
  6. "plugins": [
  7. "transform-decorators-legacy",
  8. "transform-class-properties"
  9. ]
  10. }
  • It should be noticed that transform-decorators-legacy must ahead of transform-class-properties

Documentation

Check out our Documentation page

License

This project is licensed under the terms of the MIT license