mirror of
https://github.com/sstent/node.git
synced 2026-02-01 01:51:43 +00:00
updated app
This commit is contained in:
@@ -3,6 +3,71 @@ html
|
||||
head
|
||||
title= title
|
||||
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
|
||||
script(src='http://code.jquery.com/jquery-1.6.1.min.js')
|
||||
script(src='/socket.io/socket.io.js')
|
||||
script
|
||||
$(document).ready(function() {
|
||||
var socket = io.connect('http://localhost:3000');
|
||||
|
||||
socket.on('populate', function(data) {
|
||||
var out = "";
|
||||
$.each(data, function(i, obj) {
|
||||
console.log('<li>'+obj.name+' is making '+obj.salary+'</li>');
|
||||
out += "<li>"+obj.name+" is making "+obj.salary+"</li>";
|
||||
});
|
||||
$('#employees').html(out);
|
||||
});
|
||||
|
||||
$('#btnAdd').click(function() {
|
||||
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
|
||||
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
|
||||
|
||||
// create the new element via clone(), and manipulate it's ID using newNum value
|
||||
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
|
||||
|
||||
// manipulate the name/id values of the input inside the new element
|
||||
newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
|
||||
|
||||
// insert the new element after the last "duplicatable" input field
|
||||
$('#input' + num).after(newElem);
|
||||
|
||||
// enable the "remove" button
|
||||
$('#btnDel').prop('disabled',false);
|
||||
|
||||
// business rule: you can only add 5 names
|
||||
if (newNum == 12)
|
||||
$('#btnAdd').prop('disabled',true);
|
||||
});
|
||||
|
||||
$('#btnDel').click(function() {
|
||||
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
|
||||
$('#input' + num).remove(); // remove the last element
|
||||
|
||||
// enable the "add" button
|
||||
$('#btnAdd').prop('disabled',false);
|
||||
|
||||
// if only one element remains, disable the "remove" button
|
||||
if (num-1 == 1)
|
||||
$('#btnDel').prop('disabled',true);
|
||||
});
|
||||
|
||||
$('#btnDel').attr('disabled',true);
|
||||
|
||||
$('#save').click(function() {
|
||||
|
||||
if ($('#employee_name').val() == '' || $('#employee_salary').val() == '') {
|
||||
return alert('Please enter both nam e/salary!');
|
||||
}
|
||||
var data = {
|
||||
name: $('#employee_name').val(),
|
||||
salary: $('#employee_salary').val()
|
||||
};
|
||||
console.log('socketpoking: ' + 'data');
|
||||
socket.emit('add employee', data);
|
||||
$('#employee_name').val('');
|
||||
$('#employee_salary').val('');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
body!= body
|
||||
Reference in New Issue
Block a user