@@ -10,33 +10,34 @@ angular.module('<%= scriptAppName %>').factory 'socket', (socketFactory) ->
1010 ' force new connection' : true
1111 ' max reconnection attempts' : Infinity
1212 ' reconnection limit' : 10 * 1000
13+ # Send auth token on connection
14+ # 'query': 'token=' + Auth.getToken()
1315 )
1416
15- # Send auth token on connection
16- # 'query': 'token=' + Auth.getToken()
1717 retryTimer = setInterval (->
1818 ioSocket .connect () if not ioSocket .socket .connected and not ioSocket .socket .connecting and not ioSocket .socket .reconnecting
1919 , retryInterval)
2020 socket = socketFactory (ioSocket : ioSocket)
21+
2122 socket : socket
2223
2324 ###
24- Register listeners to sync an array with a database collection through socket.io
25+ Register listeners to sync an array with updates on a model
2526
26- Takes the array we want to sync, the model namespace that socket updates are sent from,
27+ Takes the array we want to sync, the model name that socket updates are sent from,
2728 and an optional callback function after new items are updated.
2829
30+ @param {String} modelName
2931 @param {Array} array
30- @param {String} namespace
3132 @param {Function} cb
3233 ###
33- syncArray : (array , namespace , cb ) ->
34+ syncUpdates : (modelName , array , cb ) ->
3435 cb = cb or angular .noop
3536
3637 ###
3738 Syncs item creation/updates on 'model:save'
3839 ###
39- socket .on namespace + " :save" , (newItem ) ->
40+ socket .on modelName + " :save" , (newItem ) ->
4041 oldItem = _ .find (array,
4142 _id : newItem ._id
4243 )
@@ -50,12 +51,20 @@ angular.module('<%= scriptAppName %>').factory 'socket', (socketFactory) ->
5051 array .push newItem
5152 cb array
5253
53-
5454 ###
5555 Syncs removed items on 'model:remove'
5656 ###
57- socket .on namespace + " :remove" , (newItem ) ->
57+ socket .on modelName + " :remove" , (newItem ) ->
5858 _ .remove array,
5959 _id : newItem ._id
6060
61- cb array
61+ cb array
62+
63+ ###
64+ Removes listeners for a models updates on the socket
65+
66+ @param modelName
67+ ###
68+ unsyncUpdates : (modelName ) ->
69+ socket .removeAllListeners modelName + " :save"
70+ socket .removeAllListeners modelName + " :remove"
0 commit comments