1+ /** 
2+ 	Javascript client library for the Score.it api 
3+ */ 
14scoreit  =  { 
25	domain : "http://127.0.0.1:8000/" , 
36	version : "v1" , 
@@ -16,37 +19,65 @@ scoreit = {
1619
1720		return  url ; 
1821	} , 
19- 	/* 
20- 		Builds  an Ajax request. The default implementation uses the enyo framework. 
22+ 	/**  
23+ 		Makes  an Ajax request. The default implementation uses the enyo framework. 
2124		Overwrite this method to use a custom implementation. 
2225	*/ 
2326	request : function ( url ,  method ,  params ,  success ,  failure )  { 
24- 		if   ( scoreit . user . api_key )  { 
25- 			params [ "username" ]   =   scoreit . user . username ; 
26- 			 params [ "api_key" ]   =   scoreit . user . api_key ; 
27- 		 } 
27+ 		failure   =   function ( sender ,   response )  { 
28+ 			document . write ( sender . xhr . responseText ) ; 
29+ 		} ; 
30+ 
2831		var  ajax  =  new  enyo . Ajax ( { url : url ,  method : method ,  cacheBust : false ,  contentType : "application/json" } ) ; 
2932		ajax . response ( success ) ; 
3033		ajax . error ( failure ) ; 
3134		ajax . go ( params ) ; 
3235	} , 
33- 	requestResource : function ( resource ,  id ,  method ,  params ,  callback )  { 
36+ 	/** 
37+ 		Generic function for requestion resources 
38+ 	*/ 
39+ 	requestResource : function ( resource ,  id ,  method ,  params ,  callback ,  failure )  { 
3440		var  url  =  scoreit . buildResourceUrl ( resource ,  id ) ; 
35- 		scoreit . request ( url ,  method ,  params ,  callback ) ; 
41+ 
42+ 		params  =  params  ||  { } ; 
43+ 		if  ( scoreit . user . api_key )  { 
44+ 			// params["username"] = scoreit.user.username; 
45+ 			// params["api_key"] = scoreit.user.api_key; 
46+ 			url  +=  "?username="  +  scoreit . user . username  +  "&api_key="  +  scoreit . user . api_key ; 
47+ 		} 
48+ 
49+ 		params  =  JSON . stringify ( params ) ; 
50+ 		scoreit . request ( url ,  method ,  params ,  callback ,  failure ) ; 
3651	} , 
52+ 	/** 
53+ 		Validate user credentials and obtain api key and profile data 
54+ 	*/ 
3755	setUser : function ( username ,  password ,  callback )  { 
3856		scoreit . request ( scoreit . domain  +  "auth/validate/" ,  "POST" ,  { username : username ,  password : password } ,  function ( sender ,  response )  { 
3957			scoreit . user  =  response ; 
4058			scoreit . user . username  =  username ; 
4159			callback ( true ) ; 
4260		} ,  function ( sender ,  response )  { 
43- 			document . write ( sender . xhr . responseText ) ; 
4461			callback ( false ) ; 
4562		} ) ; 
4663	} 
4764} ; 
4865
49- scoreit . union  =  { 
66+ /* 
67+ 	Helper function for prototype inheritance 
68+ */ 
69+ Function . prototype . inheritsFrom  =  function ( parentClass ) { 
70+ 	this . prototype  =  new  parentClass ( ) ; 
71+ 	this . prototype . constructor  =  this ; 
72+ 	this . prototype . parent  =  parentClass . prototype ; 
73+ 	return  this ; 
74+ } ; 
75+ 
76+ /** 
77+ 	Base resource class that implements basic REST functionality 
78+ */ 
79+ scoreit . Resource  =  function ( )  { } ; 
80+ scoreit . Resource . prototype  =  { 
5081	list : function ( filters ,  callback )  { 
5182		var  params  =  { } ; 
5283		if  ( filters )  { 
@@ -58,18 +89,92 @@ scoreit.union = {
5889				params [ field ]  =  filters [ i ] [ 1 ] ; 
5990			} 
6091		} 
61- 		scoreit . requestResource ( "union" ,  null ,  "GET" ,  params ,  callback ) ; 
92+ 		scoreit . requestResource ( this . resourceName ,  null ,  "GET" ,  params ,  callback ) ; 
6293	} , 
6394	detail : function ( id ,  callback )  { 
64- 		scoreit . requestResource ( "union" ,  id ,  "GET" ,  null ,  callback ) ; 
95+ 		scoreit . requestResource ( this . resourceName ,  id ,  "GET" ,  null ,  callback ) ; 
6596	} , 
66- 	create : function ( name ,  callback )  { 
67- 		scoreit . requestResource ( "union" ,  null ,  "POST" ,  { name :  name } ,  callback ) ; 
97+ 	create : function ( params ,  callback )  { 
98+ 		scoreit . requestResource ( this . resourceName ,  null ,  "POST" ,  params ,  callback ) ; 
6899	} , 
69100	update : function ( id ,  params ,  callback )  { 
70- 		scoreit . requestResource ( "union" ,  id ,  "PATCH" ,  params ,  callback ) ; 
101+ 		scoreit . requestResource ( this . resourceName ,  id ,  "PATCH" ,  params ,  callback ) ; 
71102	} , 
72103	remove : function ( id ,  callback )  { 
73- 		scoreit . requestResource ( "union" ,  id ,  "DELETE" ,  null ,  callback ) ; 
104+ 		scoreit . requestResource ( this . resourceName ,  id ,  "DELETE" ,  null ,  callback ) ; 
74105	} 
75- } ; 
106+ } ; 
107+ 
108+ 
109+ scoreit . Union  =  function ( )  { 
110+ 	this . resourceName  =  "union" ; 
111+ } ; 
112+ 
113+ scoreit . Union . inheritsFrom ( scoreit . Resource ) ; 
114+ 
115+ scoreit . Union . prototype . create  =  function ( name ,  callback )  { 
116+ 	this . parent . create . call ( this ,  { name : name } ,  callback ) ; 
117+ } ; 
118+ 
119+ 
120+ scoreit . League  =  function ( )  { 
121+ 	this . resourceName  =  "league" ; 
122+ } ; 
123+ 
124+ scoreit . League . inheritsFrom ( scoreit . Resource ) ; 
125+ 
126+ scoreit . League . prototype . create  =  function ( name ,  gender ,  callback )  { 
127+ 	this . parent . create . call ( this ,  { name : name ,  gender : gender } ,  callback ) ; 
128+ } ; 
129+ 
130+ 
131+ scoreit . Club  =  function ( )  { 
132+ 	this . resourceName  =  "club" ; 
133+ } ; 
134+ 
135+ scoreit . Club . inheritsFrom ( scoreit . Resource ) ; 
136+ 
137+ scoreit . Club . prototype . create  =  function ( name ,  union ,  callback )  { 
138+ 	this . parent . create . call ( this ,  { name : name ,  union : union } ,  callback ) ; 
139+ } ; 
140+ 
141+ 
142+ scoreit . Team  =  function ( )  { 
143+ 	this . resourceName  =  "team" ; 
144+ } ; 
145+ 
146+ scoreit . League . inheritsFrom ( scoreit . Resource ) ; 
147+ 
148+ scoreit . Team . prototype . create  =  function ( name ,  club ,  league ,  callback )  { 
149+ 	this . parent . create . call ( this ,  { name : name ,  club : league } ,  callback ) ; 
150+ } ; 
151+ 
152+ 
153+ scoreit . Person  =  function ( )  { 
154+ 	this . resourceName  =  "person" ; 
155+ } ; 
156+ 
157+ scoreit . Person . inheritsFrom ( scoreit . Resource ( ) ) ; 
158+ 
159+ scoreit . Person . prototype . create  =  function ( firstName ,  lastName ,  passNumber ,  gender ,  address ,  city ,  zipCode ,  birthday ,  callback )  { 
160+ 	this . parent . create . call ( this ,  { first_name : firstName ,  last_name : lastName , 
161+ 		pass_number : passNumber ,  gender : gender ,  address : address ,  city : city ,  zip_code : zipCode ,  birthday : birthday } ,  callback ) ; 
162+ } ; 
163+ 
164+ 
165+ scoreit . GameType  =  function ( )  { 
166+ 	this . resourceName  =  "gametype" ; 
167+ } ; 
168+ 
169+ scoreit . GameType . inheritsFrom ( scoreit . Resource ) ; 
170+ 
171+ scoreit . GameType . prototype . create  =  function ( name ,  callback )  { 
172+ 	this . parent . create . call ( this ,  { name : name } ,  callback ) ; 
173+ } ; 
174+ 
175+ scoreit . union  =  new  scoreit . Union ( ) ; 
176+ scoreit . league  =  new  scoreit . League ( ) ; 
177+ scoreit . club  =  new  scoreit . Club ( ) ; 
178+ scoreit . team  =  new  scoreit . Team ( ) ; 
179+ scoreit . person  =  new  scoreit . Person ( ) ; 
180+ scoreit . gameType  =  new  scoreit . GameType ( ) ; 
0 commit comments