Fotis Adamakis
Fotis Adamakis

Reputation: 284

Using MVC pattern in Client and Server

I am trying for several days to find a reason to use MVC pattern in both the client and the server. I am currently using an MVC framework on the server-side and I have created a RESTfull api. I have created the templates using jsrender

<script id="housestemplate" type="text/x-jquery-tmpl">
    <li><a href="#" onclick="gethouse({{:house.id}});">
        <h3>{{:house.house_type.type}}</h3>
        <p>{{:house.area}} {{:house.bedroom_num}}</p></a>
</script>

and then I use ajax to retrieve and populate the templates

        $.ajax
        ({
            type:"GET",
            url: url ,
            success:function (data) {
                $("#renderHouse").html($("#housestemplate").render(data));
            }
        });

How can an Javascript MVC framework improve this functionality without breaking the DRY rule with models and views(templates) repeating in client and server code?

Upvotes: 1

Views: 216

Answers (1)

PhD
PhD

Reputation: 11334

For that particular example it is definitely an overkill and you are better off with simple javascript/jquery.

However, increase the amount of javascript/jquery to about 500 lines. How does it look? Manageable? How about 1000-2000 LOC? Now you'll wish that you had all code organized as objects (classes :) and methods who can talk to each other like how the backend services are organized. Hmmm...so what? Well backbone.js to the rescue! It helps organize your code into the MVC(C) paradigm (the latter C being Collection). So you get code organization and event driven communication as part of the framework and you'd have wished the prior 2k lines of code were initially organized that way to begin with.

So, what does backbone give you? Code organization and prevention from spaghetti. In fact you could use UML like analysis (class diagrams etc.) to analyze and design the 'front-end' of your application and make it more maintainable.

So the question you want to ask is, is it worth it? Well that depends on the amount of javascript you expect and the extent/size of your front-end. It does help to think of client/server as running in different realms as @jakee pointed out.

Best of luck!

Upvotes: 2

Related Questions