Skip to content

Commit

Permalink
Attach Bluebird to WebApiClient instead of exporting globally, closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalFlow committed Mar 19, 2017
1 parent bac7833 commit f6df621
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,10 @@ WebApiClient.SendRequest("POST", url, payload)
```

### Promises
This client uses bluebird for handling promises in a cross-browser compliant way.
For this reason bluebird is exported as global implementation of promises (and also overrides browser native Promises).
The decision to override native Promises was made to give you a constant usage experience across all browsers.
This client uses bluebird internally for handling promises in a cross-browser compliant way.
Therefore the promises returned by all asynchronous requests are also bluebird promises.
Bluebird itself is not exported globally anymore as of v3.0.0, but can be accessed by using ```WebApiClient.Promise```.
This decision was made for not causing issues with other scripts. CRM itself seems to be using bluebird as well (it can be found using the debugger loaded as MS default script on forms), so we don't want to inject differing versions of it, that might cause problems.

Using promises you can do something like this, too:
```Javascript
Expand All @@ -509,7 +510,7 @@ for (var i = 0; i < 5; i++) {
requests.push(WebApiClient.Create(request));
}

Promise.all(requests)
WebApiClient.Promise.all(requests)
.then(function(response){
// Process response
})
Expand Down
6 changes: 2 additions & 4 deletions src/js/WebApiClient.Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
/// <summary>Set to false for sending all requests synchronously. True by default</summary>
WebApiClient.Async = true;

// Override promise. This is for ensuring that we use bluebird internally, so that calls to WebApiClient have no differing set of
// This is for ensuring that we use bluebird internally, so that calls to WebApiClient have no differing set of
// functions that can be applied to the Promise. For example Promise.finally would not be available without Bluebird.
// In addition to that, users don't have to import it themselves for using own promises in legacy browsers.
global.Promise = require("bluebird");
var Promise = require("bluebird");

function GetCrmContext() {
if (typeof (GetGlobalContext) !== "undefined") {
Expand Down Expand Up @@ -519,7 +518,6 @@
var name = attribute.replace("@odata.nextLink", "");

// If nothing changed, this was not a deferred attribute
// !name will return true if name is empty string, which would have been a paging nextLink
if (!name || name === attribute) {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions src/js/WebApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// Get WebApiClient core
var WebApiClient = require("./WebApiClient.Core.js");

// Attach bluebird to WebApiClient
WebApiClient.Promise = require("bluebird");

// Attach requests to core
WebApiClient.Requests = require("./WebApiClient.Requests.js");

Expand Down
2 changes: 1 addition & 1 deletion src/spec/WebApiClientSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe("WebApiClient", function() {
};
requests.push(WebApiClient.Disassociate(disassociateRequest));

Promise.all(requests)
WebApiClient.Promise.all(requests)
.then(function (results){
expect(results).toBeDefined();
})
Expand Down

0 comments on commit f6df621

Please sign in to comment.