Guest User

Untitled

a guest
Jun 14th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($, undefined) {
  2.  
  3.     var methods = {
  4.         create: function () {
  5.             this.find('tr').each(function (idx, ele) {
  6.                 var row = $(ele),
  7.                     edit = row.find('a[href=#edit]'),
  8.                     remove = row.find('a[href=#remove]');
  9.  
  10.                 edit.click(function (e) {
  11.                     var target = $(e.target),
  12.                         id = target.parents('tr').children().first().data('id'),
  13.                         table = target.parents('table'),
  14.                         name = window.prompt('Name:');
  15.  
  16.                     if (!!name) {
  17.                         var value = window.prompt('Value:');
  18.                         if (!!value) {
  19.                             table.filler('edit', id, name, value);
  20.                         }
  21.                     }
  22.                     return false;
  23.                 });
  24.  
  25.                 remove.click(function (e) {
  26.                     var target = $(e.target),
  27.                         id = target.parents('tr').children().first().data('id'),
  28.                         table = target.parents('table');
  29.  
  30.                     table.filler('remove', id);
  31.                     return false;
  32.                 });
  33.             });
  34.         },
  35.  
  36.         add: function (name, value) {
  37.             var self = this;
  38.             $.ajax({
  39.                     type: 'get',
  40.                     url: 'add.txt',
  41.                     data: {name: name, value: value},
  42.                     dataType: 'json'
  43.                 }
  44.             ).done(
  45.                 function (data) {
  46.                     var row = $('<tr />'),
  47.                         cellId = $('<td />'),
  48.                         cellName = $('<td />'),
  49.                         cellValue = $('<td />'),
  50.                         cellCtrl = $('<td />'),
  51.                         edit = $('<a />'),
  52.                         remove = $('<a />');
  53.  
  54.                     cellId
  55.                         .appendTo(row)
  56.                         //.data('id', data.id)
  57.                         .attr('data-id', data.id)
  58.                         .text(data.id);
  59.  
  60.                     cellName
  61.                         .appendTo(row)
  62.                         .text(name);
  63.  
  64.                     cellValue
  65.                         .appendTo(row)
  66.                         .text(value);
  67.  
  68.                     cellCtrl
  69.                         .appendTo(row);
  70.  
  71.                     edit
  72.                         .appendTo(cellCtrl)
  73.                         .attr('href', '#edit')
  74.                         .text('[редактировать]')
  75.                         .click(function (e) {
  76.                             var target = $(e.target),
  77.                                 id = target.parents('tr').children().first().data('id'),
  78.                                 table = target.parents('table'),
  79.                                 name = window.prompt('Name:');
  80.  
  81.                             if (!!name) {
  82.                                 var value = window.prompt('Value:');
  83.                                 if (!!value) {
  84.                                     table.filler('edit', id, name, value);
  85.                                 }
  86.                             }
  87.                             return false;
  88.                         });
  89.  
  90.                     remove
  91.                         .appendTo(cellCtrl)
  92.                         .attr('href', '#remove')
  93.                         .text('[удалить]')
  94.                         .click(function (e) {
  95.                             var target = $(e.target),
  96.                                 id = target.parents('tr').children().first().data('id'),
  97.                                 table = target.parents('table');
  98.  
  99.                             table.filler('remove', id);
  100.                             return false;
  101.                         });
  102.  
  103.                     row.appendTo(self.find('tbody'));
  104.                 });
  105.         },
  106.  
  107.         edit: function (id, name, value) {
  108.             var self = this;
  109.             $.ajax({
  110.                     type: 'get',
  111.                     url: 'edit.txt',
  112.                     data: {id: id, name: name, value: value},
  113.                     dataType: 'json'
  114.                 }
  115.             ).done(
  116.                 function (data) {
  117.                     var cellId = self.find('td[data-id=' + id +']'),
  118.                         cellName = cellId.next(),
  119.                         cellValue = cellName.next();
  120.  
  121.                     cellId.text(id);
  122.                     cellName.text(name);
  123.                     cellValue.text(value);
  124.                 });
  125.         },
  126.  
  127.         remove: function (id) {
  128.             var self = this;
  129.             $.ajax({
  130.                     type: 'get',
  131.                     url: 'remove.txt',
  132.                     data: {id: id},
  133.                     dataType: 'json'
  134.                 }
  135.             ).done(
  136.                 function (data) {
  137.                     self.find('td[data-id=' + id + ']').parent().remove();
  138.                 });
  139.         }
  140.     };
  141.  
  142.     $.fn.filler = function (method) {
  143.         switch (method) {
  144.             case 'add': return methods.add.call(this, arguments[1], arguments[2]);
  145.             case 'edit': return methods.edit.call(this, arguments[1], arguments[2], arguments[3]);
  146.             case 'remove': return methods.remove.call(this, arguments[1]);
  147.             default: return methods.create.apply(this);
  148.         }
  149.     };
  150.  
  151.     $(window).ready(function () {
  152.         $('table').filler();
  153.         $('input[type=submit]').click(function (e) {
  154.             var name = $('#txtName').val(),
  155.                 value = $('#txtValue').val();
  156.  
  157.             $('table').filler('add', name, value);
  158.             return false;
  159.         });
  160.     });
  161.  
  162.     $().filler()
  163.  
  164.     var f = function (a) { this.a = a; };
  165.     f(1, 2, 3);
  166.     f.call(window, 1, 2, 3);
  167.     f.apply(window, [1, 2, 3]);
  168.  
  169. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment