Skip to content

Commit 05edb3d

Browse files
committed
pass event into callback on socket update
1 parent dc2a60f commit 05edb3d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

app/templates/client/components/socket(socketio)/socket.service(coffee).coffee

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,37 @@ angular.module('<%= scriptAppName %>').factory 'socket', (socketFactory) ->
3737
###
3838
Syncs item creation/updates on 'model:save'
3939
###
40-
socket.on modelName + ":save", (item) ->
40+
socket.on modelName + ':save', (item) ->
4141
oldItem = _.find(array,
4242
_id: item._id
4343
)
4444
index = array.indexOf(oldItem)
45+
event = 'created'
4546

4647
# replace oldItem if it exists
4748
# otherwise just add item to the collection
4849
if oldItem
4950
array.splice index, 1, item
51+
event = 'updated'
5052
else
5153
array.push item
52-
cb item, array
54+
cb event, item, array
5355

5456
###
5557
Syncs removed items on 'model:remove'
5658
###
57-
socket.on modelName + ":remove", (item) ->
59+
socket.on modelName + ':remove', (item) ->
60+
event = 'deleted'
5861
_.remove array,
5962
_id: item._id
6063

61-
cb item, array
64+
cb event, item, array
6265

6366
###
6467
Removes listeners for a models updates on the socket
6568
6669
@param modelName
6770
###
6871
unsyncUpdates: (modelName) ->
69-
socket.removeAllListeners modelName + ":save"
70-
socket.removeAllListeners modelName + ":remove"
72+
socket.removeAllListeners modelName + ':save'
73+
socket.removeAllListeners modelName + ':remove'

app/templates/client/components/socket(socketio)/socket.service.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,27 @@ angular.module('<%= scriptAppName %>')
5353
socket.on(modelName + ':save', function (item) {
5454
var oldItem = _.find(array, {_id: item._id});
5555
var index = array.indexOf(oldItem);
56+
var event = 'created';
5657

5758
// replace oldItem if it exists
5859
// otherwise just add item to the collection
5960
if (oldItem) {
6061
array.splice(index, 1, item);
62+
event = 'updated';
6163
} else {
6264
array.push(item);
6365
}
6466

65-
cb(item, array);
67+
cb(event, item, array);
6668
});
6769

6870
/**
6971
* Syncs removed items on 'model:remove'
7072
*/
7173
socket.on(modelName + ':remove', function (item) {
74+
var event = 'deleted';
7275
_.remove(array, {_id: item._id});
73-
cb(item, array);
76+
cb(event, item, array);
7477
});
7578
},
7679

0 commit comments

Comments
 (0)