Skip to content

Commit e228fe9

Browse files
committed
update documentation/name for syncCollection, and add callback
1 parent ae7de9a commit e228fe9

File tree

6 files changed

+50
-27
lines changed

6 files changed

+50
-27
lines changed

app/templates/client/app/main/main.controller(coffee).coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
angular.module('<%= scriptAppName %>').controller 'MainCtrl', ($scope, $http<% if(filters.socketio) { %>, socket<% } %>) ->
44
$http.get('/api/things').success (awesomeThings) ->
55
$scope.awesomeThings = awesomeThings
6-
<% if(filters.socketio) { %>socket.syncCollection $scope.awesomeThings, 'thing'<% } %>
6+
<% if(filters.socketio) { %>socket.syncArray $scope.awesomeThings, 'thing'<% } %>
77
<% if(filters.mongoose) { %>
88
$scope.addThing = ->
99
return if $scope.newThing is ''

app/templates/client/app/main/main.controller(js).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ angular.module('<%= scriptAppName %>')
44
.controller('MainCtrl', function ($scope, $http<% if(filters.socketio) { %>, socket<% } %>) {
55
$http.get('/api/things').success(function(awesomeThings) {
66
$scope.awesomeThings = awesomeThings;<% if(filters.socketio) { %>
7-
socket.syncCollection($scope.awesomeThings, 'thing');<% } %>
7+
socket.syncArray($scope.awesomeThings, 'thing');<% } %>
88
});
99
<% if(filters.mongoose) { %>
1010
$scope.addThing = function() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ angular.module('socketMock', []).factory 'socket', ->
1010

1111
receive: ->
1212

13-
syncCollection: ->
13+
syncArray: ->

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ angular.module('socketMock', [])
1010
receive: function() {}
1111
},
1212

13-
syncCollection: function() {}
13+
syncArray: function() {}
1414
};
1515
});

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,43 @@ angular.module('<%= scriptAppName %>').factory 'socket', (socketFactory) ->
1919
, retryInterval)
2020
socket = socketFactory(ioSocket: ioSocket)
2121
socket: socket
22-
22+
2323
###
24-
Register listeners to sync a collection with socket.io
24+
Register listeners to sync an array with a database collection through socket.io
25+
26+
Takes the array we want to sync, the model namespace that socket updates are sent from,
27+
and an optional callback function after new items are updated.
28+
29+
@param {Array} array
30+
@param {String} namespace
31+
@param {Function} cb
2532
###
26-
syncCollection: (collection, itemName) ->
27-
33+
syncArray: (array, namespace, cb) ->
34+
cb = cb or angular.noop
35+
2836
###
2937
Syncs item creation/updates on 'model:save'
3038
###
31-
socket.on itemName + ':save', (newItem) ->
32-
oldItem = _.find(collection,
39+
socket.on namespace + ":save", (newItem) ->
40+
oldItem = _.find(array,
3341
_id: newItem._id
3442
)
35-
index = collection.indexOf(oldItem)
36-
43+
index = array.indexOf(oldItem)
44+
3745
# replace oldItem if it exists
3846
# otherwise just add newItem to the collection
3947
if oldItem
40-
collection.splice index, 1, newItem
48+
array.splice index, 1, newItem
4149
else
42-
collection.push newItem
43-
50+
array.push newItem
51+
cb array
52+
53+
4454
###
4555
Syncs removed items on 'model:remove'
4656
###
47-
socket.on itemName + ':remove', (newItem) ->
48-
_.remove collection,
49-
_id: newItem._id
57+
socket.on namespace + ":remove", (newItem) ->
58+
_.remove array,
59+
_id: newItem._id
60+
61+
cb array

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,42 @@ angular.module('<%= scriptAppName %>')
3535
socket: socket,
3636

3737
/**
38-
* Register listeners to sync a collection with socket.io
38+
* Register listeners to sync an array with a database collection through socket.io
39+
*
40+
* Takes the array we want to sync, the model namespace that socket updates are sent from,
41+
* and an optional callback function after new items are updated.
42+
*
43+
* @param {Array} array
44+
* @param {String} namespace
45+
* @param {Function} cb
3946
*/
40-
syncCollection: function(collection, itemName) {
47+
syncArray: function(array, namespace, cb) {
48+
cb = cb || angular.noop;
4149

4250
/**
4351
* Syncs item creation/updates on 'model:save'
4452
*/
45-
socket.on(itemName + ':save', function(newItem) {
46-
var oldItem = _.find(collection, { _id: newItem._id });
47-
var index = collection.indexOf(oldItem);
53+
socket.on(namespace + ':save', function(newItem) {
54+
var oldItem = _.find(array, { _id: newItem._id });
55+
var index = array.indexOf(oldItem);
4856

4957
// replace oldItem if it exists
5058
// otherwise just add newItem to the collection
5159
if(oldItem) {
52-
collection.splice(index, 1, newItem);
60+
array.splice(index, 1, newItem);
5361
} else {
54-
collection.push(newItem);
62+
array.push(newItem);
5563
}
64+
65+
cb(array)
5666
});
5767

5868
/**
5969
* Syncs removed items on 'model:remove'
6070
*/
61-
socket.on(itemName + ':remove', function(newItem) {
62-
_.remove(collection, { _id: newItem._id });
71+
socket.on(namespace + ':remove', function(newItem) {
72+
_.remove(array, { _id: newItem._id });
73+
cb(array)
6374
});
6475
}
6576
};

0 commit comments

Comments
 (0)