Witam, na wstępie pragnę namienić że nie mam pojęcia jak to się robi w praktyce - przy poważnych projektach/ nie hobbistycznie.
Co chciałem uzyskać:
- przy dużych projektach, domyślam się że skryptów js jest jest od groma więc trzeba oddzielić to przestrzeniami nazw (?)
- wygodną modyfikację zapytań asynchro. oraz łatwą obsługę odpowiedzi
- przejrzystość kodu bez zbędnych objaśnień w komentarzach /**/
Podzieliłem wszystko na taką strukturę:
- namespace i obsługa dziedziczenia:
  1. /**
  2.  * namespace and extending function
  3.  */
  4. var strz_Bookmark={};
  5. (function(){
  6. strz_Bookmark.Extend=function(child, parent){
  7. for(var i in parent.prototype){
  8. if(!(i in child.prototype)){
  9. child.prototype[i]=parent.prototype[i];
  10. }
  11. }
  12. };
  13. })();
  14.  


- akcja rodzic
  1. (function(){
  2. strz_Bookmark.Action=function(rel){
  3. this.name="Action";
  4. strz_Bookmark.Action.relLink=rel;
  5. };
  6. /**
  7. * extendable container
  8. */
  9. strz_Bookmark.Action.prototype={
  10. /**
  11. * public function: jQuery on click ajax request will be send,
  12. * on recive display status
  13. */
  14. initClick:function(){
  15. var requestDone=this.onDone;
  16. var action=this.moveElement;
  17. $("a[rel='"+this.getLink()+"']").click(function(event){
  18. event.preventDefault();
  19.  
  20. $.ajax({
  21. type:'GET',
  22. self:$(this).parent(),
  23. url:this,
  24. cache:false,
  25. error: function(err) {
  26. console.log(err);
  27. switch(err.status){
  28. case 403:
  29. console.log('just login error');
  30. default:
  31. $('.content').html(err.responseText);
  32. break;
  33. }
  34. }
  35. }).done(function (data){
  36. requestDone(this.self, data, action);
  37. });
  38. });
  39. },
  40. onDone:function(self, data){},
  41. /**
  42. * privileged function: extendable
  43. */
  44. getLink:function(){
  45. return strz_Bookmark.Action.relLink;
  46. },
  47. /**
  48. * moving div
  49. */
  50. moveElement:function(view, msg, direction){
  51. direction=(direction==='right')?1:-1;
  52. view.html(msg);
  53. var endPosX=$('.left-menu').offset().left;
  54. var move=new Move(view.get(0), (view.offset().left-endPosX)*direction);
  55. },
  56. };
  57.  
  58. })();


- noi np akcja obsługująca subskrypcję (zakładki)
  1. (function(){
  2. strz_Bookmark.Subscribe=function(rel){
  3. rel= (typeof rel!=='undefined')? rel :'subscribe_bookmark';
  4. this.name="Subscribe";
  5. strz_Bookmark.Subscribe.relLink=rel;
  6. };
  7.  
  8. /**
  9. * extendable container
  10. */
  11. strz_Bookmark.Subscribe.prototype={
  12. onDone:function(self, data, moveElement){
  13. data=JSON.parse(data);
  14. //bookmark=data.bookmark;
  15. message=data.message;
  16.  
  17. moveElement(self, message, 'left');
  18. },
  19. /**
  20. * privileged function: extendable
  21. */
  22. getLink:function(){
  23. return strz_Bookmark.Subscribe.relLink;
  24. },
  25. };
  26. strz_Bookmark.Extend(strz_Bookmark.Subscribe, strz_Bookmark.Action);
  27. })();


- unsub ma inny url oraz inaczej wyświetla się informacja tej akcji więc trzeba przeładować onDone i getLink
  1. (function(){
  2. strz_Bookmark.Unsubscribe=function(rel){
  3. rel= (typeof rel!=='undefined')? rel :'delete';
  4. this.name="unsubscribe";
  5. strz_Bookmark.Unsubscribe.relLink=rel;
  6. };
  7.  
  8. /**
  9. * extendable container
  10. */
  11. strz_Bookmark.Unsubscribe.prototype={
  12. onDone:function(self, data, moveElement){
  13. data=JSON.parse(data);
  14. //bookmark=data.bookmark;
  15. message=data.message;
  16.  
  17. moveElement(self, message, 'right');
  18. },
  19. /**
  20. * privileged function: extendable
  21. */
  22. getLink:function(){
  23. return strz_Bookmark.Unsubscribe.relLink;
  24. },
  25. };
  26. strz_Bookmark.Extend(strz_Bookmark.Unsubscribe, strz_Bookmark.Action);
  27. })();


- a aktywacja akcji wygląda tak:
  1. var sub=new strz_Bookmark.Subscribe();
  2. sub.initClick();
  3.  
  4. var unsub=new strz_Bookmark.Unsubscribe();
  5. unsub.initClick();