Unfortunately, there is a lack of proper documentation on this topic as far as my research shows. However, based on the source code provided, it seems that you should be able to implement something like the following:
// Assuming you have defined **this.request** somewhere in the constructor to store the request value, which initially defaults to an empty object {};
fetchCollection: function(id) {
var that = this;
var url = 'collections/' + id;
if( Object.keys( that.request ).length !== 0 ) {
if( 'function' === typeof that.request.cancel )
that.request.cancel();
else
that.request.canceled = true;
// Resetting the value back to default {} here
that.request = {};
}
that.request.path = url;
return client( that.request ).then(
function(response){
that.collection = response.entity.data;
},
function(response){
console.error(response.status.code, response.status);
}
);
},
If this solution works for you, I can provide an explanation of why it works.
Once confirmed that the solution works, I will explain the process using pseudo-code:
Within the client( request ) function from cujojs, the following steps are taken before any action is performed:
It checks if request.canceled exists and is set to true
If the condition is met, the request is aborted
If request.canceled does not exist, the process continues to the next step
- cujojs defines a request.cancel function that can abort the request before it is sent (we do not need to focus on subsequent steps as our intention is cancellation)
We are leveraging JavaScript's ability to pass variables by reference. Passing variables by reference means that when we give the create() function a variable like request, the function uses the same variable within itself rather than creating a new copy of the variable. This manipulation allows us to directly influence the request variable used by cujojs during the execution of the create() function.
Knowing that cujojs has two ways to cancel a request, and that we can manipulate the request variable after it has been received by the create() function, we perform a simple check on our end:
We verify whether this.request is empty (indicating no previous requests have been sent yet)
If it is not empty, we check if cujojs has already defined the .cancel function on our request variable by checking 'function' === typeof request.cancel
If the function is defined, we use it to cancel the previous request. If not, we know that cujojs is moving forward to check the .canceled variable on our request, so we set it to true with this.request.canceled = true
After cancelling the request, we assign a fresh value of {} to the request variable, thereby losing reference to our previous manipulated request variable
I may not excel at explaining concepts, but I hope this breakdown clarifies the process for you. Understanding these nuances can greatly benefit your development endeavors.
Happy coding!