开发者

Mimicking objects in javascript - design decision

开发者 https://www.devze.com 2023-03-18 01:44 出处:网络
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 v

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 swapp开发者_高级运维ing 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消