Skip to content

Commit

Permalink
Merge pull request #54 from janpantel/iss-53
Browse files Browse the repository at this point in the history
Fix(once): moved once to a special function
  • Loading branch information
TheSharpieOne committed Feb 27, 2015
2 parents 951bd0d + 4e3bc8f commit 566ae5b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mock/socket-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function createMockSocketObject() {
(this._listeners[ev] = this._listeners[ev] || []).push(fn);
},
once: function(ev, fn) {
(this._raw._listeners[ev] = this._raw._listeners[ev] || []).push(fn);
(this._listeners[ev] = this._listeners[ev] || []).push(fn);
fn._once = true;
},
emit: function(ev, data) {
Expand Down
19 changes: 16 additions & 3 deletions src/service/angular-sails.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*global angular, io */
(function(angular, io) {
'use strict';
io.sails.autoConnect = false;
if(io.sails){
io.sails.autoConnect = false;
}

// copied from angular
function parseHeaders(headers) {
Expand Down Expand Up @@ -49,7 +51,7 @@

this.httpVerbs = ['get', 'post', 'put', 'delete'];

this.eventNames = ['on', 'off', 'once'];
this.eventNames = ['on', 'off'];

this.url = undefined;

Expand Down Expand Up @@ -207,7 +209,7 @@
}

function wrapEvent(eventName) {
if(socket[eventName] || socket._raw[eventName]){
if(socket[eventName] || socket._raw && socket._raw[eventName]){
socket['legacy_' + eventName] = socket[eventName] || socket._raw[eventName];
socket[eventName] = function(event, cb) {
if (cb !== null && angular.isFunction(cb)) {
Expand All @@ -219,6 +221,17 @@
}
}

// sails.io.js doesn't have `once`, need to access it through `._raw`
socket.once = function(event, cb){
if (cb !== null && angular.isFunction(cb)) {
if(socket._raw){
socket._raw.once(event, function(result) {
$rootScope.$evalAsync(cb.bind(socket, result));
});
}
}
};

angular.forEach(provider.httpVerbs, promisify);
angular.forEach(provider.eventNames, wrapEvent);

Expand Down

0 comments on commit 566ae5b

Please sign in to comment.