-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-angular.js
81 lines (78 loc) · 2.67 KB
/
socket-angular.js
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
/*jslint devel: true, browser: true*/
/*global $: true, angular: true*/
var myApp = angular.module("myApp", [])
, socket = io.connect('http://localhost:3000');
myApp.directive('contenteditable', function ($parse) {
'use strict';
// TODO : essayer avec scope: {model: "="}
return {
restrict: 'A',
link: function (scope, element, attrs) {
if (attrs.model) {
// parse pour évaluer l'expression
var model = $parse(attrs.model);
// évalue l'expression dans le contexte du scope
element.html(model(scope));
element.on('blur keyup change', function () {
// met à jour la valeur du model
model.assign(scope, element.html());
// évalue le callback dans le contexte du scope
$parse(attrs.callback)(scope);
});
}
}
}
});
function MyCtrl($scope) {
'use strict';
$scope.alerts = function (contact) {console.log(contact)}
$scope.contacts = [];
$scope.selectedContactIds = {};
$scope.setContacts = function (contacts) {
$scope.contacts = contacts;
$scope.$apply();
};
$scope.updateContact = function (contact) {
$scope.contacts.forEach(function (h,i) {
if (h.id == contact.id) {
$scope.contacts[i] = contact;
$scope.$apply();
}
});
};
$scope.sendUpdateToSocket = function (contact) {
contact = JSON.parse(angular.toJson(contact));
socket.emit('modify contact', contact);
};
$scope.selectContact = function (contact) {
$scope.selectedContactIds[$scope.user] = contact.id;
socket.emit('select contact', contact.id);
};
$scope.printSelectedBy = function (contact) {
var text = '';
angular.forEach($scope.selectedContactIds, function (contactId, user) {
if (contactId == contact.id) {
text += user;
text += ' ';
}
});
return text;
}
// $scope.$watch('selectedContacts', function (a,b) {
//
// }, true);
socket.on('login', function () {
$scope.user = prompt('Veuillez entrez votre nom :');
socket.emit('new user', $scope.user);
});
socket.on('initialize contacts', function (contacts) {
$scope.setContacts(contacts);
});
socket.on('update contact', function (contact) {
$scope.updateContact(contact);
});
socket.on('selected contact', function (data) {
$scope.selectedContactIds[data.user] = data.contactId;
$scope.$apply();
});
}