From f6df62188c6a6fcc9a10e7c4fa14c576a04b6bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=B6nert?= Date: Sun, 19 Mar 2017 15:46:20 +0100 Subject: [PATCH] Attach Bluebird to WebApiClient instead of exporting globally, closes #7 --- README.md | 9 +++++---- src/js/WebApiClient.Core.js | 6 ++---- src/js/WebApiClient.js | 3 +++ src/spec/WebApiClientSpec.js | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4762aed..c369ca9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 }) diff --git a/src/js/WebApiClient.Core.js b/src/js/WebApiClient.Core.js index a5d3c13..22fe69a 100644 --- a/src/js/WebApiClient.Core.js +++ b/src/js/WebApiClient.Core.js @@ -41,10 +41,9 @@ /// Set to false for sending all requests synchronously. True by default 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") { @@ -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; } diff --git a/src/js/WebApiClient.js b/src/js/WebApiClient.js index d936dde..ffb15f8 100644 --- a/src/js/WebApiClient.js +++ b/src/js/WebApiClient.js @@ -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"); diff --git a/src/spec/WebApiClientSpec.js b/src/spec/WebApiClientSpec.js index 6d30d75..a8eccc5 100644 --- a/src/spec/WebApiClientSpec.js +++ b/src/spec/WebApiClientSpec.js @@ -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(); })