• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

JavaScriptMVC - Model View Controller for JavaScript ...

You may have heard about the JavaScriptMVC library (it received quite a bit of press because of it's compression capabilities) and may have thought about using it, but haven't played with it yet. Well, I've been looking at the library and have found a few tutorials very useful.

View More Info

You may have heard about the JavaScriptMVC library (it received quite a bit of press because of it is compression capabilities) and may have thought about using it, but haven played with it yet. Well, I have been looking at the library and have found a few tutorials very useful.

If you haven downloaded the library you will want to do it before starting working on the tutorials (You can download the library here).

The first tutorial you should read is the getting started tutorial. This tutorial covers the basics of including the JavaScript files and the plug-in files.

Below is an excerpt from the getting started tutorial.

    Including plugins and JavaScripts

    All JavaScriptMVC functionality comes in the form of a plugin. We will include the Controller plugin, which organizes your event handlers.

    Replace myapp.html with the following HTML. Note the numbered comments, which are explained after the code.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
       <head></head>
       <body>
          <script type= ext/javascript src=jmvc/include.js></script>
          <script type= ext/javascript>
             include.plugins(controller);
    include( amyscript);
          </script>
       </body>
    </html>

       1. Creates a script tag where we will include a plugin and a script.
       2. Loads the Controller plugin.
       3. Loads myscript.js, a file we will create in the next section.

The next tutorial that I would recommend reading is the controller tutorial. The tutorial goes over the syntax that is required to use the controller.

Below is an excerpt from the controller tutorial.

    Main Controller

    If you want to respond to events like:

        * the page being loaded
        * the page being unloaded
        * the page being resized
        * the page being scrolled

    you create a special main controller by naming it amain. The main controller listens for events on the window, document, and body.

    $MVC.Controller( amain,{
      // the window onload event handler
      load: function(){
        alert(page loaded);
      },
      // the window resize event handler
      resize: function(){
        alert(page resized);
      }
    });

The next tutorial that I would read is the view tutorial. This tutorial gives an overview of how the view functionality is accomplished including the use of templates.

    Create a Template

    The first step towards cleaner code is separating your presentation markup (HTML) from your application logic (JavaScript). We will do this by separating our presentation markup into a separate file called a template. Create a file called cleaning.ejs and insert the following code:

    <h1><%= title %></h1>
    <ul>
        <% for(var i=0; i<supplies.length; i++) { %>

            <li>
                <a href= isupplies/<%= supplies[i] %>>
                    <%= supplies[i] %>
                </a>
            </li>

        <% } %>
    </ul>

    You will notice everything inside <% %> tags is
    executed and everything inside <%= %> tags inserts itself into the returned HTML string.

    We need to add JavaScript to control the loading and rendering of the template.
    We will replace the original string concatenation code with the following:

    // load the template file, then render it with data
    var html = new View({url: cleaning.ejs}).render(data);

    Doesn that feel better? The template code restores HTML to its intended structure and the JavaScript code is short and unambiguous.
    The dust is clear, now its time to mop.

It is good to see a library try to bring some form of the Model View Controller design patter to JavaScript. If you use this library for any of your projects I would love to hear about them (as usual you can leave them in the comments or write a blog post about it using your free Ajaxonomy account).

 View Full Story.
Posted at 09:20:11 am | Permalink | Posted in Javascript  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Recent Stuff

Using ASP.NET AJAX JSON Asynchronous Web Services

Growl for Windows and a Web Notification API

Learn AJAX from Scratch - Part II

Learn AJAX from Scratch - Part I

PHP AJAX CHAT, Bug fixes - including weird mozilla bug

Learn PHP From Scratch


Our Partners

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines


Search


Topics

  • .Net (96)
  • Articles (76)
  • Bookmarking (35)
  • Calendar (18)
  • Chat (38)
  • ColdFusion (3)
  • CSS (30)
  • Email (23)
  • Flash (13)
  • Games (6)
  • Google (17)
  • Html (6)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (34)
  • Javascript (146)
  • JSON (16)
  • Perl (2)
  • PHP (82)
  • Presentation (19)
  • Python (3)
  • Resources (1)
  • RSS (1)
  • Ruby (7)
  • Storage (4)
  • Toolkits (87)
  • Tutorials (190)
  • UI (11)
  • Utilities (161)
  • Web2.0 (13)
  • XmlHttpRequest (18)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange