diff --git a/bower.json b/bower.json index 3196fc9..da887ad 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-sails", - "version": "1.1.0", + "version": "1.1.1", "authors": [ "Jan-Oliver Pantel " ], diff --git a/dist/angular-sails.js b/dist/angular-sails.js index e638df8..b2fc7cb 100644 --- a/dist/angular-sails.js +++ b/dist/angular-sails.js @@ -5,7 +5,9 @@ angular.module('ngSails', ['ng']); /*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) { @@ -53,7 +55,7 @@ angular.module('ngSails', ['ng']); this.httpVerbs = ['get', 'post', 'put', 'delete']; - this.eventNames = ['on', 'off', 'once']; + this.eventNames = ['on', 'off']; this.url = undefined; @@ -211,7 +213,7 @@ angular.module('ngSails', ['ng']); } 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)) { @@ -223,6 +225,17 @@ angular.module('ngSails', ['ng']); } } + // 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); diff --git a/dist/angular-sails.min.js b/dist/angular-sails.min.js index 67c8e1c..0b0a58f 100644 --- a/dist/angular-sails.min.js +++ b/dist/angular-sails.min.js @@ -1 +1 @@ -!function(e,t){"use strict";e.module("ngSails",["ng"]),function(e,t){function o(t){var o,r,s,i={};return t?(e.forEach(t.split("\n"),function(e){s=e.indexOf(":"),o=lowercase(n(e.substr(0,s))),r=n(e.substr(s+1)),o&&(i[o]=i[o]?i[o]+", "+r:r)}),i):i}function n(t){return e.isString(t)?t.trim():t}function r(t){return t&&e.isFunction(t.then)}function s(t){var n=e.isObject(t)?t:void 0;return function(e){if(n||(n=o(t)),e){var r=n[lowercase(e)];return void 0===r&&(r=null),r}return n}}t.sails.autoConnect=!1,e.module("ngSails").provider("$sails",function(){var o=this;this.httpVerbs=["get","post","put","delete"],this.eventNames=["on","off","once"],this.url=void 0,this.urlPrefix="",this.config={transports:["websocket","polling"],useCORSRouteToGetCookie:!1},this.debug=!1;var n=this.interceptors=[];this.$get=["$q","$injector","$rootScope","$log","$timeout",function(i,u,a,c,d){function f(e){function t(){n(null)}function n(t,n){n||(n={body:t,headers:t.headers||{},statusCode:t.statusCode||t.status||0,error:function(){return this.statusCode<200||this.statusCode>=400?this.body||this.statusCode:void 0}()}),n.data=n.body,n.status=n.statusCode,n.socket=g,n.url=e.url,n.method=e.method,n.config=e.config,n.error?(o.debug&&c.warn("$sails response "+n.statusCode+" "+e.url,n),s.reject(n)):(o.debug&&c.info("$sails response "+e.url,n),s.resolve(n))}var s=i.defer();return o.debug&&c.info("$sails "+e.method+" "+e.url,e.data||""),e.timeout>0?d(t,e.timeout):r(e.timeout)&&e.timeout.then(t),g["legacy_"+e.method.toLowerCase()](e.url,e.data,n),s.promise}function l(t){g["legacy_"+t]=g[t],g[t]=function(n,r,u){var a=[f,void 0],c=i.when({url:o.urlPrefix+n,data:r,socket:g,config:u||{},method:t.toUpperCase()});for(e.forEach(p,function(e){(e.request||e.requestError)&&a.unshift(e.request,e.requestError),(e.response||e.responseError)&&a.push(e.response,e.responseError)});a.length;){var d=a.shift(),l=a.shift();c=c.then(d,l)}return c.success=function(e){return c.then(function(t){e(t.body,t.statusCode,s(t.headers),t)}),c},c.error=function(e){return c.then(null,function(t){e(t.body,t.statusCode,s(t.headers),t)}),c},c}}function h(t){(g[t]||g._raw[t])&&(g["legacy_"+t]=g[t]||g._raw[t],g[t]=function(o,n){null!==n&&e.isFunction(n)&&g["legacy_"+t](o,function(e){a.$evalAsync(n.bind(g,e))})})}var g=(t.sails&&t.sails.connect||t.connect)(o.url,o.config);g.connect=function(t){if(!g.isConnected()){var n=t||{};n=e.extend({},o.config,t),g.useCORSRouteToGetCookie=n.useCORSRouteToGetCookie,g.url=n.url||o.url,g.multiplex=n.multiplex,g._connect()}return g};var p=[];return e.forEach(n,function(t){p.unshift(e.isString(t)?u.get(t):u.invoke(t))}),e.forEach(o.httpVerbs,l),e.forEach(o.eventNames,h),g.$modelUpdater=function(t,o){g.on(t,function(t){var n;switch(t.verb){case"created":o.push(t.data);break;case"updated":var r;for(n=0;n=400?this.body||this.statusCode:void 0}()}),o.data=o.body,o.status=o.statusCode,o.socket=g,o.url=t.url,o.method=t.method,o.config=t.config,o.error?(n.debug&&c.warn("$sails response "+o.statusCode+" "+t.url,o),s.reject(o)):(n.debug&&c.info("$sails response "+t.url,o),s.resolve(o))}var s=i.defer();return n.debug&&c.info("$sails "+t.method+" "+t.url,t.data||""),t.timeout>0?f(e,t.timeout):r(t.timeout)&&t.timeout.then(e),g["legacy_"+t.method.toLowerCase()](t.url,t.data,o),s.promise}function d(e){g["legacy_"+e]=g[e],g[e]=function(o,r,u){var a=[l,void 0],c=i.when({url:n.urlPrefix+o,data:r,socket:g,config:u||{},method:e.toUpperCase()});for(t.forEach(p,function(t){(t.request||t.requestError)&&a.unshift(t.request,t.requestError),(t.response||t.responseError)&&a.push(t.response,t.responseError)});a.length;){var f=a.shift(),d=a.shift();c=c.then(f,d)}return c.success=function(t){return c.then(function(e){t(e.body,e.statusCode,s(e.headers),e)}),c},c.error=function(t){return c.then(null,function(e){t(e.body,e.statusCode,s(e.headers),e)}),c},c}}function h(e){(g[e]||g._raw&&g._raw[e])&&(g["legacy_"+e]=g[e]||g._raw[e],g[e]=function(n,o){null!==o&&t.isFunction(o)&&g["legacy_"+e](n,function(t){a.$evalAsync(o.bind(g,t))})})}var g=(e.sails&&e.sails.connect||e.connect)(n.url,n.config);g.connect=function(e){if(!g.isConnected()){var o=e||{};o=t.extend({},n.config,e),g.useCORSRouteToGetCookie=o.useCORSRouteToGetCookie,g.url=o.url||n.url,g.multiplex=o.multiplex,g._connect()}return g};var p=[];return t.forEach(o,function(e){p.unshift(t.isString(e)?u.get(e):u.invoke(e))}),g.once=function(e,n){null!==n&&t.isFunction(n)&&g._raw&&g._raw.once(e,function(t){a.$evalAsync(n.bind(g,t))})},t.forEach(n.httpVerbs,d),t.forEach(n.eventNames,h),g.$modelUpdater=function(e,n){g.on(e,function(e){var o;switch(e.verb){case"created":n.push(e.data);break;case"updated":var r;for(o=0;o