Dynamic form Builder-builder mode

Source: Internet
Author: User

When writing a popup, it can include the OK button, the Cancel button, the title bar, the Close button, the Minimize button, the content, the Maximize button and so on, but the content does not have to exist under different requirements, different requirements need to be free to mix these components, obviously each combination must be repeated coding. Separating these different, easy-to-change components and constructing complex objects step-by-stage with a builder class is the builder pattern.

The following example demonstrates the builder pattern by designing a form builder. Now the popular Hybrid app development network request is basically through AJAX implementation, imagine if the application requires different user roles to submit different forms, then we need a dynamic generation of forms. We can encapsulate different form controls into control classes, and then use the Builder class to combine different spaces as required to form the desired form.

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/8B/72/wKiom1hOV4vz98nLAAAk76Nh1tw434.png-wh_500x0-wm_3 -wmp_4-s_3218903375.png "title=" builder mode. png "alt=" wkiom1hov4vz98nlaaak76nh1tw434.png-wh_50 "/>


Var formcontrol = class.extend ({ctor:function (_label,_name,_id,_type) {this.label = _ Label;this.name = _name;this.id = _id;this.type = _type;},tostring:function () { return this.label +  "<input type = "  + this.type +  "'  id =  '  + this.id +  '  name =  '  + this.name  +  "'/>";}}); Var button = formcontrol.extend ({ctor:function (_label,_name,_id,_callback) {This._super (_label,_ name,_id, "button"); This.callback = _callback;},tostring:function () {return  "<input   value =  '  + this.label +  '  type =  '  +  this.type +  "'  id =  '"  + this.id +  " name = "  + this.name +  "'  onclick =  '"  + this.callback +  "() '/ > "}}); var input = formcontrol.extend ({}); Var textarea = formcontrol.extend ({toString: function () {return  this.label +  "<textarea name = "  +  this.name + " id = "  + this.id +  " />";}); Var select = formcontrol.extend ({ctor:function (_label,_name,_id,_type,_options) {This._super (_ Label,_name,_id,_type); This.options = _options;},tostring:function () {var txt =  ""; for (var i = 0;i < this.options.length;i++) {txt +=  "<option value = "  + this.options[i].value +  ">"  + this.options[i].text +  "</option>";} return this.label +  "<select name = "  + this.name +  "'  id =  '  + this.id +  ' > '  + txt +  ' </select> ';}}); Var formbuilDer = class.extend ({controls:[],ctor:function (_id,_controls = []) {this.id = _id; This.controls = _controls;},addcontrol:function (_control) {This.controls.push (_control);return  This;},build:function () {Var form = document.getelementbyid (this.id); var form_txt =   ""; for (var i = 0;i < this.controls.length;i++) {form_txt +=  This.controls[i].tostring ();} Form.innerhtml = form_txt;}});

Application code

Effect:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/8B/72/wKiom1hOWCSwBob2AAAj-7K5BQk586.png-wh_500x0-wm_3 -wmp_4-s_4138527962.png "title=" builder mode effect. png "alt=" wkiom1howcswbob2aaaj-7k5bqk586.png-wh_50 "/>

Other Notes:

The builder model and the factory model can be combined to delegate the production of the component to the factory model, which is completed by the builder mode.

The advantages of the builder model: the relative independence of the components (the above example, because of the possible deviation of style layout, the form in the mobile Web is often the form of a list, so it can be relatively independent), easy to expand.

Cons of Builder mode: components must have similarities, and complex applications may require too many builders.

This article from "Go one stop two look back three" blog, please make sure to keep this source http://janwool.blog.51cto.com/5694960/1881979

Dynamic form Builder-builder mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.