简体   繁体   中英

Mimicking objects in javascript - design decision

I seem to often be making the decision to create objects in javascript that are basically the ModelView or Model objects in an MVC architecture. So when the user changes their properties or whatever via the webpage, the javascript object's properties are changed, and then the javascript object is submitted to the controller. The main reasons I am using this approach are:

  • Increased flexibility of display. Alternatively, everything is done with forms so that I can easily post them back to the controller. Forms do not allow various display options such as wrapping around other elements and so on.

  • I am using a lot of javascript anyway. I have to manipulate the elements whenever buttons are pressed (like swapping two list elements around). I also usually hijack the buttons and use them with ajax.

  • The default model binder rarely works on complex objects so I need to maintain two sets of objects anyway.

  • It is easier to perform complex validation on a javascript object compared to html.

Are there any libraries or techniques or approach that assist with this approach? Maybe JSON.NET? Do any people try to avoid this approach?

I haven't taken this approach myself but there are some frameworks that sound similar to what you are proposing. The one that looks most interesting is Knockout JS . It provides data binding to your model among other features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM