-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
117 lines (106 loc) · 2.85 KB
/
main.js
File metadata and controls
117 lines (106 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var app = angular.module('app', []);
app.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true); // enable pushState
$routeProvider.when('/', {
templateUrl: '/app.html',
controller: 'AppCtrl'
});
$routeProvider.when('/another/route', {
templateUrl: '/deep.html',
controller: 'AppCtrl'
});
});
app.factory('socket', ['$rootScope', function ($rootScope) {
var socket = io.connect();
return {
on: function (eventName, callback) {
function wrapper() {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
}
socket.on(eventName, wrapper);
return function () {
socket.removeListener(eventName, wrapper);
};
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if(callback) {
callback.apply(socket, args);
}
});
});
}
};
}]);
Array.maxProp = function (array, prop) {
var values = array.map(function (el) {
return el[prop];
});
console.log(Math.max.apply(Math, values));
return Math.max.apply(Math, values);
};
app.controller('AppCtrl', ['$scope', 'socket', function ($scope, socket) {
socket.on('sendCustomer', function (data) {
console.log('send customer');
$scope.customers=data;
});
$scope.model = {
message: 'Customer Data Management'
};
$scope.customers = [];
$scope.id = '';
$scope.add = 'Add';
$scope.name = '';
$scope.getCustomer = function() {
socket.emit('updateCustomer', $scope.customers);
console.log("$scope.customers");
console.log($scope.customers);
};
$scope.addCustomer = function(name, id) {
console.log(name);
if(name==''){
alert("name can't be blank");
return;
}
if(id==''){
var max_id = Array.maxProp($scope.customers, 'id');
//~ console.log(max_id);
//~ console.log(typeof(max_id));
if(!isFinite(max_id))max_id=0;
$scope.customers.push({"name":name,id:max_id+1});
}else {
console.log($scope.customers);
for(var i=0; i<$scope.customers.length; i++) {
if($scope.customers[i].id==id){
$scope.customers[i].name=name;
}
}
$scope.add="Add";
$scope.id='';
}
console.log($scope.customers);
$scope.name='';
//~ console.log($scope.customers);
socket.emit('updateCustomer', $scope.customers);
};
$scope.editCustomer = function(name, id) {
$scope.name=name;
$scope.id=id;
$scope.add="Edit";
};
$scope.delCustomer = function(id) {
if( confirm('are you sure to delete')){
for(var i=0; i<$scope.customers.length; i++) {
if($scope.customers[i].id==id){
$scope.customers.splice(i,1);
}
}
socket.emit('updateCustomer', $scope.customers);
}
};
}]);