(function ($, undefined) {
var methods = {
create: function () {
this.find('tr').each(function (idx, ele) {
var row = $(ele),
edit = row.find('a[href=#edit]'),
remove = row.find('a[href=#remove]');
edit.click(function (e) {
var target = $(e.target),
id = target.parents('tr').children().first().data('id'),
table = target.parents('table'),
name = window.prompt('Name:');
if (!!name) {
var value = window.prompt('Value:');
if (!!value) {
table.filler('edit', id, name, value);
}
}
return false;
});
remove.click(function (e) {
var target = $(e.target),
id = target.parents('tr').children().first().data('id'),
table = target.parents('table');
table.filler('remove', id);
return false;
});
});
},
add: function (name, value) {
var self = this;
$.ajax({
type: 'get',
url: 'add.txt',
data: {name: name, value: value},
dataType: 'json'
}
).done(
function (data) {
var row = $('<tr />'),
cellId = $('<td />'),
cellName = $('<td />'),
cellValue = $('<td />'),
cellCtrl = $('<td />'),
edit = $('<a />'),
remove = $('<a />');
cellId
.appendTo(row)
//.data('id', data.id)
.attr('data-id', data.id)
.text(data.id);
cellName
.appendTo(row)
.text(name);
cellValue
.appendTo(row)
.text(value);
cellCtrl
.appendTo(row);
edit
.appendTo(cellCtrl)
.attr('href', '#edit')
.text('[редактировать]')
.click(function (e) {
var target = $(e.target),
id = target.parents('tr').children().first().data('id'),
table = target.parents('table'),
name = window.prompt('Name:');
if (!!name) {
var value = window.prompt('Value:');
if (!!value) {
table.filler('edit', id, name, value);
}
}
return false;
});
remove
.appendTo(cellCtrl)
.attr('href', '#remove')
.text('[удалить]')
.click(function (e) {
var target = $(e.target),
id = target.parents('tr').children().first().data('id'),
table = target.parents('table');
table.filler('remove', id);
return false;
});
row.appendTo(self.find('tbody'));
});
},
edit: function (id, name, value) {
var self = this;
$.ajax({
type: 'get',
url: 'edit.txt',
data: {id: id, name: name, value: value},
dataType: 'json'
}
).done(
function (data) {
var cellId = self.find('td[data-id=' + id +']'),
cellName = cellId.next(),
cellValue = cellName.next();
cellId.text(id);
cellName.text(name);
cellValue.text(value);
});
},
remove: function (id) {
var self = this;
$.ajax({
type: 'get',
url: 'remove.txt',
data: {id: id},
dataType: 'json'
}
).done(
function (data) {
self.find('td[data-id=' + id + ']').parent().remove();
});
}
};
$.fn.filler = function (method) {
switch (method) {
case 'add': return methods.add.call(this, arguments[1], arguments[2]);
case 'edit': return methods.edit.call(this, arguments[1], arguments[2], arguments[3]);
case 'remove': return methods.remove.call(this, arguments[1]);
default: return methods.create.apply(this);
}
};
$(window).ready(function () {
$('table').filler();
$('input[type=submit]').click(function (e) {
var name = $('#txtName').val(),
value = $('#txtValue').val();
$('table').filler('add', name, value);
return false;
});
});
$().filler()
var f = function (a) { this.a = a; };
f(1, 2, 3);
f.call(window, 1, 2, 3);
f.apply(window, [1, 2, 3]);
})(jQuery);