Created
October 24, 2014 11:13
-
-
Save HAKASHUN/1d9f90f5373da261726b to your computer and use it in GitHub Desktop.
$qにspread()を追加する
From https://egghead.io/lessons/angularjs-chained-promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ( window ){ | |
"use strict"; | |
/** | |
* Decorate the $q service instance to add extra | |
* `spread()` and `resolve()` features | |
*/ | |
var $QDecorator = function ($provide) | |
{ | |
// Partial application to build a resolve() function | |
var resolveWith = function( $q) | |
{ | |
return function resolved( val ) | |
{ | |
var dfd = $q.defer(); | |
dfd.resolve( val ); | |
return dfd.promise; | |
}; | |
}; | |
// Register our $log decorator with AngularJS $provider | |
$provide.decorator('$q', ["$delegate", | |
function ($delegate) | |
{ | |
if ( angular.isUndefined( $delegate.spread )) | |
{ | |
// Let's add a `spread()` that is very useful | |
// when using $q.all() | |
$delegate.spread = function( targetFn,scope ) | |
{ | |
return function() | |
{ | |
var params = [].concat(arguments[0]); | |
targetFn.apply(scope, params); | |
}; | |
}; | |
} | |
if ( angular.isUndefined( $delegate.resolve )) | |
{ | |
// Similar to $q.reject(), let's add $q.resolve() | |
// to easily make an immediately-resolved promise | |
// ... this is useful for mock promise-returning APIs. | |
$delegate.resolve = resolveWith($delegate); | |
} | |
return $delegate; | |
} | |
]); | |
}; | |
if ( window.define != null ) | |
{ | |
window.define([ ], function ( ) | |
{ | |
return [ "$provide", $QDecorator ]; | |
}); | |
} else { | |
window.$QDecorator = [ "$provide", $QDecorator ]; | |
} | |
})( window ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment