homepage Welcome to WebmasterWorld Guest from 54.226.192.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript MVC Pattern
Best Practices
cabbagehead

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3528743 posted 1:28 am on Dec 16, 2007 (gmt 0)

After reviewing a few JavaScript libraries for AJAX development, I've quickly come to realize the event model seems to imply use of the MVC pattern for development. Assuming you accept this premise, the next question is how to best implement this pattern.

Here are my thoughts and I'd appreciate any feedback/debate you might want to throw into the mix:

1. Model - pure domain-model Javascript objects that deal with the actual behaviors to be invoked.

1. View - a pure DOM (zero CSS or Javascript or styling info; purely a div-centric DOM definition.

2. Controller - Observer pattern implementation around the idea of even registration and listeners that observe those events.

So something I'm wonder is - should the controller code be in a seperate JS include from the model code for that "Page"? I could see where one could say that either (a) the controller code is the one set of javascript code that *does* belong in a script tag in the head of the DOM and acts to connect the DOM to the model objects. Or (b) that it belongs in its own events class, in a seperate file.

Any thoughts, suggestions, opinions, etc?

Thanks.
N

 

cabbagehead

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3528743 posted 3:51 am on Dec 17, 2007 (gmt 0)

Anyone? :)

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3528743 posted 5:12 pm on Dec 17, 2007 (gmt 0)


So something I'm wonder is - should the controller code be in a seperate JS include from the model code for that "Page"? I could see where one could say that either (a) the controller code is the one set of javascript code that *does* belong in a script tag in the head of the DOM and acts to connect the DOM to the model objects. Or (b) that it belongs in its own events class, in a seperate file.

I'm not sure I totally understand your MVC mapping. With that said, I generally like to have one main script block per page... I suppose that would be the Controller. That block might create instances of certain objects... I suppose those would be the Model. But as for the View? I'm not really sure there really is a "view" layer. Typically, the Model objects might be totally self contained widgets, in which case most of the view information is contained within those 'classes'.

For example, my page head might look something like this:

<script type="text/javascript" src="WIDGET1.js"></script>
<script type="text/javascript" src="WIDGET2.js"></script>
<script type="text/javascript" src="THISPAGE.js"></script>

So the first 2 scripts there might contain what I would consider to be Model (and some View code) and the last one would contain all of the code for creating instances of those widgets (ie - the controller).

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved