tags:

views:

112

answers:

1

I'm about to make a web app which will have a pretty heavy client end. I'm not sure about the way to organize my javascript code, but here is a basic idea :

// the namespace for the application
var app = {}; 

// ajax middle layer
app.products = {
  add : function(){
    // send ajax request 
    // if response is successful
       // do some ui manipulation
       app.ui.products.add( json.data );
  },
  remove : function(){},
  ... 
};
app.categories = {
  add : function(){},
  ....
};

// the ui interface which will be called based on ajax responses
app.ui = {};
app.ui.products = {
  add : function( product_obj ){
     $('#products').append( "<div id='"+product_obj.id+"'>"+product_obj.title+"</div>" );
  }
};
app.ui.categories = {};

Anybody got similar experiences to tell me the pros and cons of this approach? What's your way of designing client side javascript code architecture? Thanks.

[update] : This web app, as you see from the above, deals with products CRUD, categories CRUD only in a ajax fashion. I'm only showing an snippet here, so you guys know what I'm trying to achieve and what my question is. Again, I'm asking for inputs for my approach to organize the code of this app.

+3  A: 

That is similar to the way I do my JavaScript projects. Here are some tricks I have used:

  • Create one file for each singleton object. In your code, store ajax middle layer and ui interface in separate files
  • Create a global singleton object for the 3 layers usually in the project; GUI, Backend and App
  • Never use pure ajax from anywhere outside the Backend object. Store the URL to the serverside page in the Backend object and create one function that uses that URL to contact the server.
  • Have a JSON class on the server that can report errors and exceptions to the client. In the Backend object, check if the returned JSON object contains an error, and call the serverError function in the GUI class to present the error to the user (or developer).

Here is an example of a Backend object:

var Backend = {};
Backend.url = "/ajax/myApp.php";
Backend.postJSON = function(data, callback){
  var json = JSON.stringify(data);
  $.ajax({
    type: "POST",
    url: Backend.url,
    data: "json="+json,
    dataType: "json",
    success: function(response){
      if(response){
        if(response.task){
          return callback(response);
        }else if(response.error){
          return Backend.error(response);
        }
      } 
      return Backend.error(response);
    },
    error: function(response){
      Backend.error({error:"network error", message:response.responseText});
    },
  });
};
Backend.error = function(error){
  if(error.message){
    Client.showError(error.message, error.file, error.line, error.trace);
  }
};

This can be improved by storing the ajax object somewher in the Backend object, but it's not necessary.

Marius
@Marius: Could you be more specific about "Store the URL to the serverside page in the Backend object and create one function that uses that URL to contact the server.". Thanks. Other points I totally agree.
Shawn
@Marius: Got it now. I was taking a similar approach in my previous projects. The rule here I think is to use only one layer for client-server data streaming.
Shawn
@Shawn: I guess Marius idea is to have a centric place to handle communication.
Codism
@Codism : That's what I meant in the comment. Oh my poor English...
Shawn