Learning how to use JSON to get rid of the tedious work of Bidirectional conversion between forms and Business Objects

Source: Internet
Author: User
  1. I think all my colleagues who process form programs will feel bored. When displaying data, they need to bind business objects to forms one by one, when processing the submitted form, the fields contained in the form must be bound to the business object one by one. This process is cumbersome and unpleasant to modify.
    In the past, form handlers basically extended such a routine (Here C # is used as an example, and other languages are roughly the same ):
    TextBoxPersonName. Text = person. Name;
    TextBoxPersonBirthday. Text = person. Birthday. ToString ("yyyy-MM-dd ");
    Int index = 0;
    For (int I = 0; I <dtBirthplac. Rows. Count; I ++ ){
    If (dtBirthplac. Rows [I] ["Code"]. ToString () = person. Birthplac ){
    Index = I;
    Break;
    }
    }
    DropdownListPersonBirthplace. SelectedIndex = index;
    ......
    Or the code that is processed directly on the template is as follows (the template language of Velocity is roughly the same here ):
    <Input id = "PersonName" name = "PersonName" value = "$! Person. Name "/>
    <Input id = "Birthday" name = "Birthday" value = "$! Person. Birthday. ToString ("yyyy-MM-dd") "/>
    # Foreach ($ r in $ dtBirthplac. Rows)
    # If ($ r. Code = $ person. Birthplac)
    <Option value = "$ r. Code" selected = "selected"> $ r. Desc </option>
    # Else
    <Option value = "$ r. Code"> $ r. Desc </option>
    # End
    # End
    The process of submitting a form is equally cumbersome, and type conversion is often involved. The Code is as follows:
    Person. Name = request ["Name"];
    Person. Birthplace = int. Parse (request ["Birthplace"]);
    ......
  2. Now
    If you can automate the two-way binding of objects (that is, you can convert objects into forms and directly convert forms into objects ). Objects in Javascript are flexible and can be modified at any time using code. You can easily bind fields on the client using js. The client can use the same javascrui for processing. For example:
    Var person = {
    Name: 'lisq ',
    Birthday: '20140901'
    }
    <Form id = "formPerson">
    <Input name = "Name"/>
    <Input name = "Birthplace"/>
    </Form>
    <Script>
    For (p in person ){
    $ (P). value = person [p]
    }
    </Script>
    The form is also submitted from the traditional form. convert submit () to: Convert the Form to json format (refer to the Form object for Prototype implementation of json js), and then submit the Form to integrate the ajax application. The Code is as follows:
    <Script>
    Var p = formPerson. serialize (true)
    Var paras = 'o = '+ p. toJSONString ()
    Request (url, paras, function (){
    Alert ('saved successfully! ')
    })
    </Script>
    The Field Obtained by the server is only O, not the previous Name, Birthplace. O is the converted string {Name: 'lisq', Birthday: '123 '}, the server converts Json to C # Object. Person p = JSON. serialize (request ["O"], typeof (Person) (refer to the C # Implementation of json). If the data access layer has a good implementation, you can add a line of code to achieve p. save () (see nhib.pdf ). So good !!!

Of course, there will be a lot of details in this two-way binding, especially the reverse resolution of the server-side json, Which is returned from the json format string to the server and needs to be implemented according to different platform languages. But in any case, this is also an attempt, hoping to inspire the development of form programs in the future.

Related Article

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.