SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about OOP 'flow'

    Hello,

    I am currently 'translating' a web app from 'normal' javascript to OOP javascript.

    I have read a lot of theory about functions, objects, methods, inheritance and so on.

    But now I am thinking more about what routine to use.

    Basicly, I used to have a lot of functions to setup my UI:

    checkForFirstLaunch();
    loadLanguage();
    loadNavigation();
    loadImages();

    I would just call checkForFirstLaunch(), and if it returned false, I would go to the next step, loadLanguage(), and at the end of that function I would call loadNavigation().

    But now, I am using just one object named setup, which contains methods like checkForFirstlaunch, loadLanguage etc.

    Here's an example:

    Code:
    var setup = {
    
    	run: function() {
    	
    		// We check if the app has just been launched for the first time. If true, we have to start downloading some images.
    	
    		( ls.get( "start" ) == null ) ? this.downloadImages() : this.loadLanguage();
    	
    	},
    
    	loadLanguage: function () {
    	
    		...
    		
    		loadNavigation();
    	
    	},
    	
    	...
    
    }
    But this doesn't look very sexy, and I feel I am not really using much of the OOP potential here, just translating functions into methods. I thought about perhaps having the methods return true if they where successful, but then my code would just be clustered with if/else code. So what's a good way to control the 'flow' of an application?

    Thanks!

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript is OOP. Practically everything is an object.

    I think what you are looking for is more of an automated way of doing this. GOOD FOR YOU! You can do this:

    Code:
    var myScript  = {
      init: function() {
        //this would need to change but I'll leave it as so you can at least get the concept
        var isReady = true;
    
        if (isReady) {
          for (var x in this) {
            //dont run init more than once and only functions!
            if (x != "init" && typeof x == "function") {
              x(); //lets run all the functions
            }
          }
        }
      },
    
      loadLanguage: function() {
        //etc...
      },
    
      feelSexy: function() {
        //make the function feel sexy
      }
    };


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •