123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- $.define(["jQuery", "util"], "form", function($, util) {
- var impls = [],
- data_key = "jfw_base_form",
-
- di = function(val) {
- var v = val,
- rules = [],
- dv = null;
- return {
- get: function() {
- return v;
- },
- set: function(vd) {
- v = vd;
- },
- reset: function() {
- v = dv;
- },
- validate: function() {
- return util.validate(rules, this);
- },
- addRules: function(rule) {
- util.addRules(rules, rule);
- },
- valid: function() {},
- invalid: function(reson) {}
- };
- },
- vd = function(items) {
- for(key in items) {
- if(true!==items[key].validate())
- return false;
- }
- return true;
- },
- serialize=util.serialize,
-
- bf = function($e,itemOptions) {
- if($e.length === 1) {
- var items = {},
- rules = [];
- $e.find(".form-item").each(function() {
- var $this = $(this);
- for(var i = 0; i < impls.length; ++i) {
- var item = impls[i]($this,itemOptions);
- if(item && item.name) {
- items[item.name] = item;
- }
- }
- });
- return {
- item: function(name) {
- return items[name];
- },
- validate: function() {
- if(vd(items)) {
- return util.validate(rules, this);
- }
- return false;
- },
- addRules: function(rule) {
- var te = $.type(rule);
- if("function" === te) {
- rules.push(rule);
- } else if("array" === te) {
- rule.forEach(function(item){
- rules.push(item);
- });
- } else if("object" === te) {
- for(key in rule) {
- var im = items[key];
- if(im) im.addRules(rule[key]);
- }
- }
- },
- val: function(data) {
- if(arguments.length) {
- if(data) {
- for(key in data) {
- var ch = items[key];
- if(!ch) {
- ch = items[key] = di();
- }
- ch.set(data[key]);
- }
- }
- return this;
- }
- var ret = {};
- for(key in items) {
- var tmp = items[key].get();
- if(undefined !== tmp) {
- ret[key] = tmp;
- }
- }
- return ret;
- },
- reset: function() {
- for(key in items) {
- items[key].reset();
- }
- },
- get: function(url, data, eh, config) {
- util.get(url, data, function(rd) {
- if(config && config.check) {
- rd = config.ckeck(rd);
- if(rd) {
- this.reset();
- this.val(rd);
- }
- }
- }, eh, config);
- },
- post: function(url, data, eh, config) {
- util.post(url, data, function(rd) {
- if(config && config.check) {
- rd = config.ckeck(rd);
- if(rd) {
- this.reset();
- this.val(rd);
- }
- }
- }, eh, config);
- },
- doGet: function(url, sh, eh, config) {
- if(this.validate()) {
- util.get(url, serialize(this.val()), sh, eh, config);
- }
- },
- doPost: function(url, sh, eh, config) {
- if(this.validate()) {
- util.post(url, serialize(this.val()), sh, eh, config);
- }
- },
- doPut: function(url, sh, eh, config) {
- if(this.validate()) {
- util.put(url, this.val(), sh, eh, config);
- }
- }
- };
- }
- return null;
- };
- $.fn.form = function(val) {
- if(this.length && this.length ===1) {
- return bf(this);
- }
- };
- return {
- build: bf,
- register: function(impl) {
- impls.push(impl)
- }
- };
- });
|