Practices for domain models in Javascript (with frameworks)
Posted
by
AndyBursh
on Programmers
See other posts from Programmers
or by AndyBursh
Published on 2013-09-15T19:52:50Z
Indexed on
2013/11/07
22:20 UTC
Read the original article
Hit count: 224
This is a question I've to-and-fro'd with for a while, and searched for and found nothing on: what're the accepted practices surrounding duplicating domain models in Javascript for a web application, when using a framework like Backbone or Knockout?
Given a web application of a non-trivial size with a set of domain models on the server side, should we duplicate these models in the web application (see the example at the bottom)? Or should we use the dynamic nature to load these models from the server?
To my mind, the arguments for duplicating the models are in easing validation of fields, ensuring that fields that expected to be present are in fact present etc. My approach is to treat the client-side code like an almost separate application, doing trivial things itself and only relying on the server for data and complex operations (which require data the client-side doesn't have). I think treating the client-side code like this is akin to separation between entities from an ORM and the models used with the view in the UI layer: they may have the same fields and relate to the same domain concept, but they're distinct things.
On the other hand, it seems to me that duplicating these models on the server side is a clear violation of DRY and likely to lead to differing results on the client- and server-side (where one piece gets updated but the other doesn't). To avoid this violation of DRY we can simply use Javascripts dynamism to get the field names and data from the server as and when they're neeed.
So: are there any accepted guidelines around when (and when not) to repeat yourself in these situations? Or this a purely subjective thing, based on the project and developer(s)?
Example
Server-side model
class M
{
int A
DateTime B
int C
int D = (A*C)
double SomeComplexCalculation = ServiceLayer.Call();
}
Client-side model
function M(){
this.A = ko.observable();
this.B = ko.observable();
this.C = ko.observable();
this.D = function() { return A() * C(); }
this.SomeComplexCalculation = ko.observalbe();
return this;
}l
M.GetComplexValue = function(){
this.SomeComplexCalculation(Ajax.CallBackToServer());
};
I realise this question is quite similar to this one, but I think this is more about almost wholly untying the web application from the server, where that question is about doing this only in the case of complex calculation.
© Programmers or respective owner