Skip to content

magocto/Octopusjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

       .oOo.          O                                  O       
       O   o         oOo                                         
       O   O  .oOo    o   .oOo. .oOo. O   o  .oOo       'o .oOo  
       O   O  O       O   O   o O   o o   O  `Ooo.       O `Ooo. 
       o   O  o       o   o   O o   O O   o      O  oO   o     O 
       `OoO'  `OoO'   `oO `OoO' oOoO' `OoO'o `OoO'  Oo   O `OoO' 
                                O                        o       
                               o'                      oO'    

octopus.js is a object composition library that allows you to create object classes with traits (sometimes referred to tentacles). We can build our objects picking from the build your own menu.
we can say

	var MegaClass = Octopus.spawn(["events", "stateMachine", "view", "viewContainer", "lockable"], {
		construct:function(locked, elementid) {
			this.sm.setState("Idle");
			this.setLocked(locked);
			this.view.setEl($("#"+elementid));
		},
		doSomeMegaClassStuff:function() {
	
		}
	}

	var m1 = new MegaClass(true, "someElementId");
	m1.setState("gearing up");
	m1.doSomeMegaClassStuff();

This allows us to create a object that has events, a state-machine, has a view itself, holds other views, and is lockable. 

#================== NOTE ======================#
you can also call Octopus by its short form _8

	 var models = _8.path("myapp.models");
	 var EventTimer = _8.spawn(["events, timer"]);

*----------------------------------------------------------------------------------*
*  Tentacles/Traits and object composition:                                        *
*----------------------------------------------------------------------------------*

Maximize you code reuse by using tentacles/traits to be grafted onto you objects 

	
In this example we have a library that can check out books to students, other libraries, and the restoration shop. I know, just go with it.

	
create a tentacle for the borrowing functionality:

	Octopus.t("borrower", {
		// this function will be called when the object that it is grafted onto is constructed
		construct:function() {
			
			this._checkedOut = [];
		},
		checkout:function(book) {
			this._checkedOut.push(book);
		}
	}});
	
	Octopus.t("scheduled", {
		setSchedule:function(schedule) {
			this.schedule = schedule;
		}
	}})
	

now we can create our student class

	var Student = Octopus.spawn("borrower, scheduled, otherStudentTrait", {
		// this will be called after all traits have been initialized 
		construct:function(name, schedule) {
			console.log("create student "+id+" ");
			this.setSchedule(schedule);
			
			// all new students live in redthorn hall no matter what for some reason...
			this.moveIn("redthorn hall");
		},
		// adding student specific functions
		moveIn:function(dorm) {
			this.dorm = dorm;
		}
	});


now we can make as many students want

	var st = new Student("jethro", {"biology":"104", "welding":"201"});


adding Backbone Events to an object

	_8.t("events", Backbone.Events);
	
	var PostOffice = _8.spawn("events, goverment, employer");
	var hollowStation = new PostOffice();
	hollowStation.bing("mail", function() {
		readMail();
	});

    
*----------------------------------------------------------------------------------*
*  Namespacing:                                                                    *
*----------------------------------------------------------------------------------*

As applications get larger staying organized gets harder. Trying to balance not polluting the global scope while keeping every object accessible for testing gets tedious.
You can use octopus paths to simplify namespacing your applications various modules.

   Octopus.path("myapp.models", {
      _books:[];
       BookModel:function(title, author) {
   		    this.title = title;
   		    this.author = author;
       },
       addBook:function(title, author) {
   		    this._books.push(new this.Bookmodel(title, Author);
       }
    });

somewhere else in your app:

	var models = Octopus.path("myapp.models");
	models.addBook("Smashed, Squashed, Splattered, Chewed, Chunked and Spewed", "Lance Carbuncle");





*----------------------------------------------------------------------------------*
*  Dependency Injection:                                                           *
*----------------------------------------------------------------------------------*


                                                                                
                                                                                
                                                   OOO8$ZZI.                    
                                                MOD8OZZ$O7Z7I.                  
                                              .OZZZZZZO77$OZ8Z.                 
                                              ZZOZZ$$Z$7$ZZ7O$D.                
                                            .OOZZZ7$ZO7I7$ZO7ZZ.                
                                            OO$Z$$$7??$$7$$Z$.=.                
                                I8++O$=.   .7=OZZI7O7$$$ZOO8OI?.                
                            .~I?7+:,OO+I$  OOO$7Z7$Z$O$ZZZDZ$7..                
                            ?:?~87N...$+:.DZ87$Z77+I=ZO8O$7ZZO..                
                           .D=,=8,.   .+?.ZO7$$$8ZZ$Z+7OOOI~=.                  
                           ~7?=8Z   7=+??$7$Z$ZO77$O$7$ZZ77I..                  
             ..            $+7O$O   .... .7$$$O$$N$7O7ZZ7+M.,:8~.O..            
           ~IIZ7$+.        .N?O7O8.     .ZZOZ$87ZOD$OZI?.,?$~+=~=+IZ            
         +I7IZODZO7,       .:$+?ZZN.  .8=OOOZ$OZODDD+,.$.=+O=~+788~~..          
        $777D.   .$I        ?IO$8IO$$7Z~I7I8Z$$$ON8??DD8+:7I$8ZDO++?+.          
        =I$$Z.   ..7?.      .+NDZ8I7Z+Z?Z$Z8ZOZ$$$,?NN8~:,OD88DI~$??~. .        
        ?77777,,   .O8..    .8OO8$$O$?I7$88IO?$8$78::D88I=IODOZOO+?,7$?.,..     
        .=IO7ZO$Z$$$787ZDOZOZDOOD$~$I7ZZD$,77+~$8IOOMNZ8D$D=8ZOZ?D.N$+I$,I=.    
         .=I?+I77OOOZ7$7I7+Z78O$~DZ?+O7ZZO87Z7==88$Z8N8OZ7OZ7OOZ. .I7,.?=7+.    
     .      ~M+$IIII+?77+$8$88$O7IOZOZ$D7$7$?+Z7I8DDODM8Z8$OZM.   .77..OII..    
        ..II..   ...,=MMOM$Z$$7Z8DN87$OO87Z7ID?7OZZZO?Z7N877M      +?OO=???.    
      .Z==~.:7           .MZ$NN.IZO=$I7I7I7$ODZI8$~Z8Z$OIZDNIM...N$88$I==I.     
    .+=~~?Z7I~     .....~,Z?$7D7?7~I?$IZ?I?~Z?7OI?8,$DZ$7$7IDZ7?$IIOIO:=~..     
   .D=,=8. I+.   ..I8ZO$ZI7O$77I$Z$$,7$$7Z7$:8ZOIIOI$777II8O$~7ZI77::I8Z.       
   D?~$8..?Z...DODOO77Z$D7Z$IOZD8$.ZI+$O88Z:7+Z77?7+DIZZ77I7ZO77.?~Z,.          
 ..?IZD~,?..O88DI=?:7OZ$8D$$$OOZ787O7Z8$ZZ$7Z?7?Z8?D$?I$$ZN7MM8M.               
 .8+:$88OZOZ$Z$$7Z~7$7??=IMMMM8OZO$?IDZ?7$$88877?+$Z7$7$$D.                     
 .7I~8D87ZIZZ$??=NI,.        ..M?87$O?D7I?MM~78O8$??OZ$O$O.                     
  .7~I=87+II+N~.          .N    M8?8I+$$?IM. MMOI?DOOI7Z?DOZ8Z$$Z$Z...          
   .O?:+$Z.                ?I.  .$,7II:78M.    MMMI78D$$$N?Z~7$I$II+O$..        
                           .:~.. M87$I?7?M.     .:MM:Z887IOD8....:M877DM.       
                           .+7.  .MZ77?D$M.         ZM$ZOI7ZOO$DDDM$?ZZ$.       
                           I,I.    7O$OZ7$.           M$?Z$8O77IZ?7+I7Z7.       
                         .:?=?     .8$+???.           .M=$ZZNI$$:7$I7$?~        
                         .,I??     .D87O$$.              ?O+I7$$OZOIOO.         
                         ..?,II.....8$~O$8.                 ..+MMMM.            
                         .+I,+$D$=O$7ZZ8I.                                      
                           =$:O+ZOI$?II.M                                       
                            ?8.,:I:I.ON~                                        
                              IM=$M~M+.                                         
                                                                

About

A object composition library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published