Failure to work with the breeze

I have a simple angularjs service that wraps the wind to make it available to my application. In this service, I want one processing engine not to execute. I tried putting .fail () in a chain at the end of the call, and if the breeze call failed (due to the user logging out, for example), fail is called. The only problem is that the first .done () in the chain (usually located in the controller that calls the service) is also called. I just want it to be called when the wind is big.

How to prevent the first called from a call?

Below is an example of a select request (with an incorrect call)

dataStore.saveEntity(model)
    .then(function() {
    // This is being called on fail             
});

And a fragment of the service wrapping

angular.module('app')
.factory('dataStore' ,function() {

        var _handleFail = function(error) {
            if (error && error.status && error.status === 401) {
                // Logged out error (for example

            }
        };



        function saveEntity(entity) {   
           return manager.saveChanges(entity).fail(_handleFail);    
        }
+4
3

, .fail() . :

promise.then(function(){
      console.log('Then 1');
      throw new Error('Error');
  })
  .then(function(){
      console.log('Then 2');
  })
  .then(function(){
      console.log('Then 3');   
  })
  .fail(function(){
      console.log('Fail 1');   
  })
  .then(function(){
      console.log('Then 4');
  });

:

> Then 1
> Fail 1
> Then 4

.Fail() .then(), .then(). , .fail(), , .then() .

, _handleFail() , .then() ( , .fail().

, :

angular.module( '') .factory('dataStore', function() {

    var _handleFail = function(error) {
        if (error && error.status && error.status === 401) {
            // Logged out error (for example
            throw new Error('Unauthorized');
        }
    };



    function saveEntity(entity) {   
       return manager.saveChanges(entity).fail(_handleFail);    
    }
+3

, "fail" promises "then", , . :

function handleFail(error) {
    if (error && error.status && error.status === 401) {
        // Logged out error (for example

    }
};


dataStore.saveEntity(model)
 .then(function() {
    // your success code here. - should not get called except on success.
 }).fail(handleFail);

angular.module('app')
   .factory('dataStore' ,function() {

        function saveEntity(entity) {   
           return manager.saveChanges(entity);
        }
    });

, .

0

Jay , , 100% , Angular , , , - ( , , , Angular, ) -

var status = ko.observable(true);    
// var $scope.status = true (maybe???)
dataStore.saveEntity(model)
    .then(function() {
        if (status()) { // status still equals true, no failures occurred, carry on 
        }
});


angular.module('app')
.factory('dataStore' ,function() {
    var _handleFail = function(error) {
        if (error && error.status && error.status === 401) {
            // Logged out error (for example)
            status(false);
        }
    };

    function saveEntity(entity) {   
       return manager.saveChanges(entity).fail(_handleFail);    
    }

() , then() , then() , , .

0

Source: https://habr.com/ru/post/1502682/


All Articles