Skip to content

Instantly share code, notes, and snippets.

@leeight
Created September 26, 2017 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeight/f9136c12d2a27cc5fad7bfc10aa736d6 to your computer and use it in GitHub Desktop.
Save leeight/f9136c12d2a27cc5fad7bfc10aa736d6 to your computer and use it in GitHub Desktop.
About san validate

打算写的时候变成这样子

<ui-form rules="{{rules}}">
  <ui-formitem><input type="text" name="name" /></ui-formitem>
  <ui-formitem><input type="password" name="password" /></ui-formitem>
</ui-form>

其中

var Form = san.defineComponent({
  template: '<form class="san-form"><slot/></form>',
  messages: {
    'form-element-changed': function (arg) {
      var rules = this.data.get('rules');
      // TODO
    }
  },
  attached: function () {
    // TODO
  }
});

var FormItem = san.defineComponent({
  template: '<div class="san-form-item"><slot/></div>',
  attached: function () {
    var self = this;
    var child = this.slotChilds[0].childs[0];
    var name = child.evalExpr(child.props.get('name').expr));
    if (!name) {
      return;
    }
    if (isComponent(child)) {
      child.on('input', function () {
        self.dispatch('form-element-changed', {
          name: name,
          value: child.evalExpr(child.props.get('value').expr))
        });
      });
    }
    else {
      child._onEl('input', function () {
        self.dispatch('form-element-changed', {
          name: name,
          value: child.el.value
        });
      });
    }
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment