xpepermint / vue-rawmodel

RawModel.js plugin for Vue.js v2. Form validation has never been easier!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decorator support

kristianmandrup opened this issue · comments

Allow for fields static method

http://aliolicode.com/2016/05/07/typescript-static-members/

https://github.com/xpepermint/vue-rawmodel/blob/master/src/models.ts#L29

public constructor (data: ReactiveModelRecipe = {}) {
    super(data);
    // ...
    let clazz = this.constructor;
    fields = fields || typeof clazz.fields === 'function' ? clazz.fields() : clazz.fields
    if (fields) this.defineFields(fields)
  }
import {ReactiveModel} from 'vue-rawmodel';

class User extends ReactiveModel {
  constructor (data = {}) {
    super(data); // initializing parent class
  }

  static fields() {
    return {
      name: {
        type: 'String', // setting type casting
        validate: [{
            validator: 'presence', // validator name
            message: 'is required' // validator error message
          }
        ]
      }
    };
  }
}

Equivalent to calling

  constructor (data = {}) {
    super(data); // initializing parent class
    this.defineField({
      // ...
    }
  }
commented

A note on the side: 😄

If "ReactiveModel" is a fitting name, it (or VueReactiveModel) is sexier than VueRawModel. 😉

Scott

vue-class-component is an official companion library that allows you to declare components as native JavaScript classes, with a @component decorator.

Based on this quote from the official Vue.js documentation, I decided to add decorator support. Coming soon ...