ordercloud-api / ordercloud-javascript-sdk

The official Javascript SDK for the OrderCloud eCommerce platform
MIT License
16 stars 15 forks source link

Javascript promises not behaving as expected #1

Closed robertsoniv closed 7 years ago

robertsoniv commented 7 years ago

When using this SDK in an angular controller, my response body comes back into the .then() just fine where I can log it to my console; however, if I attempt to set the response body data to a variable in my controller the angular scope is not updated.

vm.search = function() {
    OrderCloud.Buyers.List(vm.parameters)
        .then(function(data) {
            console.log(data)  // this logs to the console okay, but has that weird "exports" thing
            vm.result = data; // vm.result is not updated
        })
}

I believe there must be some difference in how promises are handled in angular vs. raw javascript.

Interesting side note, I do not have this problem when using the new SDK in a ui-router resolve - the data populates into the controller just fine in this case.

robertsoniv commented 7 years ago

JavaScript promises are a wonderful tool that are still fairly new. Because it’s not native to all browsers yet, most js frameworks have to create a custom integration that keeps track of promises and any events they create. This also means that the promises created by Parse’s SDK do not fire the Angular digest cycle when they resolve. The following is an example controller implementation of the repository we just created. The one major problem? The $scope array is never rendered to the view.

Seems like I might have to beef up the angular wrapper for the SDK to fix this problem.

ghost commented 7 years ago

fixed in oc-wrapper