4 ms·
Pure MVC isn't possible for most Javascript apps, because the Model lives on the server. Now we're talking something like MMVC (Model-server, model-client, view
by jerf 17y ago
Pure MVC isn't possible for most Javascript apps, because the Model lives on the server. Now we're talking something like MMVC (Model-server, model-client, view, controller). Oh, and the controller isn't just controlling the view, it eventually has to feedback to the actual server model, so it's more like MMVCC. Oh, and if you want the page to load concrete HTML for the initial view of the data (for speed, since dynamically constructing it client side may be slow), then we've got MMVVCC.
Now, I know some sites don't have that last one, but MMVCC is pretty common.
Now that following MMVVCC has mucked up your code beyond belief, maybe you should reconsider whether it actually bought you anything.
Upshot: The MVC abstraction just doesn't make sense in this context. It cuts the wrong way for a client-server application, and if you're just forcing your code into MVC's structure because there's this dogma floating around that it's a good idea... don't do that! There are far better ways to structure your JS. In fact, almost anything is a better way to structure your JS.
- sfphotoarts 17y agono, MVC is perfectly possible. Think of the server model as the persistence layer then it will become clear to you.
- known 17y agobingo
- 9oliYQjP 17y agoI can see your point and certainly I've seen code as you have described. However, just because the model lives on a server doesn't mean that you can't have a model class on the client. If that were true, then we would not be able to have model classes tied to databases. But that's fairly common, even if it does add a level of abstraction. The big mistake that folks make is not that they use MVC on the client but that they use MVC on the server side in this scenario. You'd get better bang for your buck if you used MVC on the client and accessed RESTful resources via a model class, and treated the server like a filesystem for loading controller and view classes that were handled exclusively by the client.
- mjw 17y agoGenuinely interested here in what you'd propose as an alternative. Separating model logic from presentational logic helps me a lot in client side code. Then the 'two models' problem seems inescapable if you want to divide the load of model-level computation between client and server. Sure, if you do all the non-trivial computation server-side, then the client-side model becomes less important. But sometimes you need to do more complex computation in the client, whether for UI latency or scalability reasons. If you didn't, you'd just be writing a traditional server-side web app (which of course is fine, but not what we're talking about). On the other hand, if you do most of the computation client-side, the server-side can be a relatively trivial persistence layer, and sometimes that works out really well. But even if all you're doing is calling on a persistence layer, it can still require a fair bit of bookkeeping logic in your client-side model code. versioning, caching and cache expiry, object identity, managing consistency of local copies of denormalised data etc. As for 'two controllers', I'm not sure I get what you mean there. In most verisons of MVC I've seen, the controller talks to both model and view, not just the view. But it is a poorly-defined term. Personally I prefer an approach something like "M + Tree(V + C)". That is, a model layer, and then a tree of 'widgets' or user-interface components, each of which has its own controller and view logic. Widgets may create and manage child widgets. etc. Finding this works quite well, especially when you throw data-binding into the mix too. Agree that the 'VV' approach you describe sounds quite a crazy optimisation, depending on how far you take it. Although might make more sense if you have more of a unified development environment between client and server eg GWT or a javascript-based server framework.
- jerf 17y ago"Genuinely interested here in what you'd propose as an alternative." Honestly? Read the motivations for MVC. Read carefully about the reason why some people think it's a good idea, because I think the ideas are valuable. Then... just write your code, and focus on keeping it well-factored, not MVC. MVC is really about keeping your code well-factored, but it only applies literally in a handful of circumstances. The ideas apply everywhere, but you're better off understanding how to keep things well-factored than how to jam things into MVC. MVC is giving a man a fish, understanding factoring is teaching them how to fish, but the metaphor breaks down here in that it's actually easier to learn how to fish here. Replying to this with an elaborate explanation of when MVC applies would be missing the point. If it is the best-factored organization, then go for it. But it is the best solution fairly rarely, where the best may be MMVC, or MVCC, or MVC+tree, or, frankly, endless variations on the theme. There's so many valid variations of such diversity that viewing everything through the lens of MVC just isn't that helpful.
- nreece 17y agoPure MVC isn't possible for most Javascript apps, because the Model lives on the server. Not entirely true. It actually depends on the nature of the application. In some cases, where feasible, you can easily use a Client-Side Persistent Storage or Google Gears as the Model.