angular-observe
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -76,25 +76,32 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var currentState; | ||
var subscription; | ||
var isolateScope = $rootScope.$new(true, $scope); | ||
var source = $scope.$eval($attrs.asyncBind || $attrs.source); | ||
var sourceModel = $attrs.asyncBind || $attrs.source; | ||
$element.empty(); | ||
if (!source) { | ||
console.warn('The `observable` directive requires a source observable.'); | ||
return; | ||
} | ||
setState('loading'); | ||
// Subscribe to the observable | ||
var observable = typeof source.subscribe === 'function' | ||
? source | ||
: typeof source.then === 'function' | ||
? liftPromise(source) | ||
: liftValue(source); | ||
var subscription = observable.subscribe(onNext, onError, onComplete); | ||
$scope.$watch(sourceModel, function (source) { | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
} | ||
setState('loading'); | ||
// Lift the source to an Observable-compatible interface | ||
var observable = typeof source.subscribe === 'function' | ||
? source | ||
: typeof source.then === 'function' | ||
? liftPromise(source) | ||
: liftValue(source); | ||
// Subscribe to the observable | ||
subscription = observable.subscribe(onNext, onError, onComplete); | ||
}); | ||
// Unsubscribe when this element is destroyed | ||
$scope.$on('$destroy', function() { | ||
subscription.unsubscribe(); | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
} | ||
}); | ||
@@ -162,8 +169,28 @@ | ||
subscribe: function (onNext, onError, onComplete) { | ||
source.then(onNext, onError, onNext) | ||
.catch(function (reason) { | ||
return reason; | ||
}) | ||
.then(onComplete); | ||
// I don't want to depend on any single Promise implementation | ||
// so I can't rely on re-throwing the error. Instead, I guard | ||
// on `failed` for the invocation of `onComplete`. | ||
var failed = false; | ||
var _onNext = function (val) { | ||
onNext(val); | ||
return val; | ||
}; | ||
var _onError = function (val) { | ||
onError(val); | ||
failed = true; | ||
}; | ||
var _onComplete = function () { | ||
if (!failed) { | ||
onComplete(); | ||
} | ||
}; | ||
source.then(_onNext, _onError, onNext) | ||
.then(_onComplete); | ||
return { | ||
@@ -179,2 +206,5 @@ unsubscribe: Angular.noop, | ||
subscribe: function (onNext, onError, onComplete) { | ||
// We are already using Angular so let's just leverage | ||
// an existing service (`$timeout`) to fire the `onNext` and | ||
// `onComplete` callbacks asynchronously. | ||
$timeout(onNext.bind(null, source), 0, false) | ||
@@ -236,3 +266,2 @@ .then(onComplete); | ||
} | ||
}]); | ||
@@ -239,0 +268,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r(require("angular")):"function"==typeof define&&define.amd?define(["angular"],r):"object"==typeof exports?exports.AngularObserve=r(require("angular")):e.AngularObserve=r(e.angular)}(this,function(e){return function(e){function r(t){if(n[t])return n[t].exports;var o=n[t]={exports:{},id:t,loaded:!1};return e[t].call(o.exports,o,o.exports,r),o.loaded=!0,o.exports}var n={};return r.m=e,r.c=n,r.p="",r(0)}([function(e,r,n){var t=n(1);e.exports="filearts.angularObserve";var o=t.module(e.exports,[]);o.directive("asyncBind",["$compile","$q","$rootScope","$timeout",function(e,r,n,o){function u(e){var r=a(e);return function(e,t,o){function u(e){function r(e){var r=typeof e;return"boolean"===r||"number"===r||"string"===r}var n=!r(v.$value)||!r(e);v.$value=e,f("active",n)}function a(e){f("complete")}function s(e){v.$error=e,f("error")}function f(e,n){if(n||e!==p){var o=r[e];if(l&&l.$destroy(),!o)return void t.empty();l=v.$new(),p=e,o(l,function(e){t.empty(),t.append(e)})}v.$root.$$phase||v.$digest(!0)}var l,p,v=n.$new(!0,e),d=e.$eval(o.asyncBind||o.source);if(t.empty(),!d)return void console.warn("The `observable` directive requires a source observable.");f("loading");var b="function"==typeof d.subscribe?d:"function"==typeof d.then?c(d):i(d),m=b.subscribe(u,s,a);e.$on("$destroy",function(){m.unsubscribe()})}}function c(e){return{subscribe:function(r,n,o){return e.then(r,n,r)["catch"](function(e){return e}).then(o),{unsubscribe:t.noop}}}}function i(e){return{subscribe:function(r,n,u){return o(r.bind(null,e),0,!1).then(u),{unsubscribe:t.noop}}}}function a(r){var n={},o={loading:[],active:[],error:[],complete:[]},u=[];t.forEach(r.contents(),function(e){switch(e.nodeName.toLowerCase()){case"loading":return o.loading.push(e);case"active":return o.active.push(e);case"error":return o.error.push(e);case"complete":return o.complete.push(e);default:return u.push(e)}});var c=!1;if(t.forEach(o,function(r,o){if(r.length){c=!0;var u=t.element(r).clone();n[o]=e(u)}}),!c){u.length||u.push(document.createTextNode("{{$value}}"));var i=t.element(u).clone();n.active=e(i),n.complete=e(i.clone())}return n}return{restrict:"EA",scope:!0,compile:u}}])},function(r,n){r.exports=e}])}); | ||
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n(require("angular")):"function"==typeof define&&define.amd?define(["angular"],n):"object"==typeof exports?exports.AngularObserve=n(require("angular")):e.AngularObserve=n(e.angular)}(this,function(e){return function(e){function n(t){if(r[t])return r[t].exports;var o=r[t]={exports:{},id:t,loaded:!1};return e[t].call(o.exports,o,o.exports,n),o.loaded=!0,o.exports}var r={};return n.m=e,n.c=r,n.p="",n(0)}([function(e,n,r){var t=r(1);e.exports="filearts.angularObserve";var o=t.module(e.exports,[]);o.directive("asyncBind",["$compile","$q","$rootScope","$timeout",function(e,n,r,o){function u(e){var n=a(e);return function(e,t,o){function u(e){function n(e){var n=typeof e;return"boolean"===n||"number"===n||"string"===n}var r=!n(v.$value)||!n(e);v.$value=e,f("active",r)}function a(e){f("complete")}function s(e){v.$error=e,f("error")}function f(e,r){if(r||e!==l){var o=n[e];if(p&&p.$destroy(),!o)return void t.empty();p=v.$new(),l=e,o(p,function(e){t.empty(),t.append(e)})}v.$root.$$phase||v.$digest(!0)}var p,l,d,v=r.$new(!0,e),b=o.asyncBind||o.source;t.empty(),e.$watch(b,function(e){d&&d.unsubscribe(),f("loading");var n="function"==typeof e.subscribe?e:"function"==typeof e.then?c(e):i(e);d=n.subscribe(u,s,a)}),e.$on("$destroy",function(){d&&d.unsubscribe()})}}function c(e){return{subscribe:function(n,r,o){var u=!1,c=function(e){return n(e),e},i=function(e){r(e),u=!0},a=function(){u||o()};return e.then(c,i,n).then(a),{unsubscribe:t.noop}}}}function i(e){return{subscribe:function(n,r,u){return o(n.bind(null,e),0,!1).then(u),{unsubscribe:t.noop}}}}function a(n){var r={},o={loading:[],active:[],error:[],complete:[]},u=[];t.forEach(n.contents(),function(e){switch(e.nodeName.toLowerCase()){case"loading":return o.loading.push(e);case"active":return o.active.push(e);case"error":return o.error.push(e);case"complete":return o.complete.push(e);default:return u.push(e)}});var c=!1;if(t.forEach(o,function(n,o){if(n.length){c=!0;var u=t.element(n).clone();r[o]=e(u)}}),!c){u.length||u.push(document.createTextNode("{{$value}}"));var i=t.element(u).clone();r.active=e(i),r.complete=e(i.clone())}return r}return{restrict:"EA",scope:!0,compile:u}}])},function(n,r){n.exports=e}])}); |
{ | ||
"name": "angular-observe", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Angular directive for representing the different states of an Observable.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/angular-observe.js", |
@@ -20,25 +20,32 @@ var Angular = require('angular'); | ||
var currentState; | ||
var subscription; | ||
var isolateScope = $rootScope.$new(true, $scope); | ||
var source = $scope.$eval($attrs.asyncBind || $attrs.source); | ||
var sourceModel = $attrs.asyncBind || $attrs.source; | ||
$element.empty(); | ||
if (!source) { | ||
console.warn('The `observable` directive requires a source observable.'); | ||
return; | ||
} | ||
setState('loading'); | ||
// Subscribe to the observable | ||
var observable = typeof source.subscribe === 'function' | ||
? source | ||
: typeof source.then === 'function' | ||
? liftPromise(source) | ||
: liftValue(source); | ||
var subscription = observable.subscribe(onNext, onError, onComplete); | ||
$scope.$watch(sourceModel, function (source) { | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
} | ||
setState('loading'); | ||
// Lift the source to an Observable-compatible interface | ||
var observable = typeof source.subscribe === 'function' | ||
? source | ||
: typeof source.then === 'function' | ||
? liftPromise(source) | ||
: liftValue(source); | ||
// Subscribe to the observable | ||
subscription = observable.subscribe(onNext, onError, onComplete); | ||
}); | ||
// Unsubscribe when this element is destroyed | ||
$scope.$on('$destroy', function() { | ||
subscription.unsubscribe(); | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
} | ||
}); | ||
@@ -106,8 +113,28 @@ | ||
subscribe: function (onNext, onError, onComplete) { | ||
source.then(onNext, onError, onNext) | ||
.catch(function (reason) { | ||
return reason; | ||
}) | ||
.then(onComplete); | ||
// I don't want to depend on any single Promise implementation | ||
// so I can't rely on re-throwing the error. Instead, I guard | ||
// on `failed` for the invocation of `onComplete`. | ||
var failed = false; | ||
var _onNext = function (val) { | ||
onNext(val); | ||
return val; | ||
}; | ||
var _onError = function (val) { | ||
onError(val); | ||
failed = true; | ||
}; | ||
var _onComplete = function () { | ||
if (!failed) { | ||
onComplete(); | ||
} | ||
}; | ||
source.then(_onNext, _onError, onNext) | ||
.then(_onComplete); | ||
return { | ||
@@ -123,2 +150,5 @@ unsubscribe: Angular.noop, | ||
subscribe: function (onNext, onError, onComplete) { | ||
// We are already using Angular so let's just leverage | ||
// an existing service (`$timeout`) to fire the `onNext` and | ||
// `onComplete` callbacks asynchronously. | ||
$timeout(onNext.bind(null, source), 0, false) | ||
@@ -180,3 +210,2 @@ .then(onComplete); | ||
} | ||
}]); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21196
391