- 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"]);
......
- 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.