Advertisement
stixlink

Comments

Nov 24th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var CommentsController = function () {
  2.  
  3.     var self = this;
  4.  
  5.     this.url = 'index.php?r=comments/createAjax';
  6.     this.form = null;
  7.     this.body = null;
  8.     this.container = null;
  9.     this.notify = null;
  10.  
  11.     this.appendComment = function (data) {
  12.  
  13.         var html = self.generateHtml(data);
  14.  
  15.         jQuery(this.body).prepend(html);
  16.  
  17.         jQuery('html, body').animate({
  18.                 scrollTop: (jQuery(this.body).offset().top - 50)
  19.             },
  20.             1000);
  21.         jQuery('.comment:first', this.body).slideDown(750);
  22.         jQuery('.no-comments', this.container).slideUp(500);
  23.     }
  24.  
  25.  
  26.     this.generateHtml = function (data) {
  27.  
  28.         var res = '<div class="comment" style="display: none;"> \
  29.                     <div class="author"> \
  30.                         Я \
  31.                     </div> \
  32.                     <div class="created"> \
  33.                          ' + data.created + '\
  34.                     </div> \
  35.                     <div class="text"> \
  36.                         ' + data.comment + ' \
  37.                     </div> \
  38.                 </div>';
  39.  
  40.         return res;
  41.     };
  42.  
  43.  
  44.     this.sendRequest = function (data) {
  45.  
  46.         jQuery.post(
  47.             this.decodeUrlPath(self.url),
  48.             {
  49.                 'ajax': data,
  50.                 'ieV': this.getIEValidator()
  51.             },
  52.             self.sendCallback,
  53.             'json'
  54.         );
  55.     };
  56.  
  57.  
  58.     this.sendCallback = function (response) {
  59.  
  60.         if (response.status) {
  61.  
  62.             self.appendComment(response.records);
  63.             self.form.get(0).reset();
  64.         }
  65.         else {
  66.  
  67.             self.notify.raiseMessage('Не удалось сохранить комментарий.');
  68.         }
  69.  
  70.     };
  71.  
  72.     this.collectData = function () {
  73.  
  74.         var res = {};
  75.  
  76.         res['comment'] = jQuery('textarea', self.form).val();
  77.         res['id_order'] = jQuery('input[name="Comments[id_order]"]', self.form).val();
  78.  
  79.         return res;
  80.     };
  81.  
  82.  
  83.     this.init = function () {
  84.  
  85.         jQuery('.comments .submit span').click(function (e) {
  86.  
  87.             var data = self.collectData();
  88.             self.sendRequest(data);
  89.         });
  90.  
  91.         this.container = jQuery('.comments');
  92.  
  93.         this.form = jQuery('.foot form', this.container);
  94.         this.body = jQuery('.body', this.container);
  95.  
  96.  
  97.         this.notify = window.market.notifyManger;
  98.     }
  99.  
  100.  
  101.     this.init();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement