Sin Descripción

form.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. $.define(["jQuery", "util"], "form", function($, util) {
  2. var impls = [],
  3. data_key = "jfw_base_form",
  4. /* default impl */
  5. di = function(val) {
  6. var v = val,
  7. rules = [],
  8. dv = null;
  9. return {
  10. get: function() {
  11. return v;
  12. },
  13. set: function(vd) {
  14. v = vd;
  15. },
  16. reset: function() {
  17. v = dv;
  18. },
  19. validate: function() {
  20. return util.validate(rules, this);
  21. },
  22. addRules: function(rule) {
  23. util.addRules(rules, rule);
  24. },
  25. valid: function() {},
  26. invalid: function(reson) {}
  27. };
  28. },
  29. vd = function(items) {
  30. for(key in items) {
  31. if(true!==items[key].validate())
  32. return false;
  33. }
  34. return true;
  35. },
  36. serialize=util.serialize,
  37. /* create form instance by jQuery obj */
  38. bf = function($e,itemOptions) {
  39. if($e.length === 1) {
  40. var items = {},
  41. rules = [];
  42. $e.find(".form-item").each(function() {
  43. var $this = $(this);
  44. for(var i = 0; i < impls.length; ++i) {
  45. var item = impls[i]($this,itemOptions);
  46. if(item && item.name) {
  47. items[item.name] = item;
  48. }
  49. }
  50. });
  51. return {
  52. item: function(name) {
  53. return items[name];
  54. },
  55. validate: function() {
  56. if(vd(items)) {
  57. return util.validate(rules, this);
  58. }
  59. return false;
  60. },
  61. addRules: function(rule) {
  62. var te = $.type(rule);
  63. if("function" === te) {
  64. rules.push(rule);
  65. } else if("array" === te) {
  66. rule.forEach(function(item){
  67. rules.push(item);
  68. });
  69. } else if("object" === te) {
  70. for(key in rule) {
  71. var im = items[key];
  72. if(im) im.addRules(rule[key]);
  73. }
  74. }
  75. },
  76. val: function(data) {
  77. if(arguments.length) {
  78. if(data) {
  79. for(key in data) {
  80. var ch = items[key];
  81. if(!ch) {
  82. ch = items[key] = di();
  83. }
  84. ch.set(data[key]);
  85. }
  86. }
  87. return this;
  88. }
  89. var ret = {};
  90. for(key in items) {
  91. var tmp = items[key].get();
  92. if(undefined !== tmp) {
  93. ret[key] = tmp;
  94. }
  95. }
  96. return ret;
  97. },
  98. reset: function() {
  99. for(key in items) {
  100. items[key].reset();
  101. }
  102. },
  103. get: function(url, data, eh, config) {
  104. util.get(url, data, function(rd) {
  105. if(config && config.check) {
  106. rd = config.ckeck(rd);
  107. if(rd) {
  108. this.reset();
  109. this.val(rd);
  110. }
  111. }
  112. }, eh, config);
  113. },
  114. post: function(url, data, eh, config) {
  115. util.post(url, data, function(rd) {
  116. if(config && config.check) {
  117. rd = config.ckeck(rd);
  118. if(rd) {
  119. this.reset();
  120. this.val(rd);
  121. }
  122. }
  123. }, eh, config);
  124. },
  125. doGet: function(url, sh, eh, config) {
  126. if(this.validate()) {
  127. util.get(url, serialize(this.val()), sh, eh, config);
  128. }
  129. },
  130. doPost: function(url, sh, eh, config) {
  131. if(this.validate()) {
  132. util.post(url, serialize(this.val()), sh, eh, config);
  133. }
  134. },
  135. doPut: function(url, sh, eh, config) {
  136. if(this.validate()) {
  137. util.put(url, this.val(), sh, eh, config);
  138. }
  139. }
  140. };
  141. }
  142. return null;
  143. };
  144. $.fn.form = function(val) {
  145. if(this.length && this.length ===1) {
  146. return bf(this);
  147. }
  148. };
  149. return {
  150. build: bf,
  151. register: function(impl) {
  152. impls.push(impl)
  153. }
  154. };
  155. });