0

I'm having some trouble using the AngularJS framework. I have multiple views (ui-router) with controllers bound to them (ofc). Some of them are referring to the same service, some aren't. This is e.g. my userFactory:

function UserFactory () {
var factory = {};

factory.signIn = function (header) {
    return new Promise (function (resolve, reject) {
        $.when(
            $.ajax({
                type: 'POST',
                url: me.baseUrl + 'api/users/login',
                headers: {
                    'Authorization': header
                }
            })
        ).then(
            function() {
                resolve();
            }, 
            function () {
                reject();
            }
        );
    });
}

factory.getUserInformation = function (header) {
    return new Promise(function (resolve, reject) {
        $.when(
            $.ajax({
                type: 'GET',
                url: me.baseUrl + 'api/users/get',
                headers: {
                    'Authorization': header
                }
            })
        ).then(
            function (user) {
                resolve(user);
            },
            function () {
                reject();
            }
        );
    })
}

return factory;
};

On loading the SPA, there are 2 views using this service. They both retrieve the user information (factory.getUserInformation()). This results in 2 HTTP requests that are retrieving exactly the same information.

In this same service I have method that makes it possible to remove a product from the basket. Now here is the problem: I have another view that displays the user's his/hers basket. So I created an EventEmitter object that tells the Basket-view to update itself. This initiates a HTTP request again. Is there some way to kind of making the Basket-view notice the data has changed?

factory.removeProductFromBasket = function (productId, header) {
    return new Promise(function (resolve, reject) {
        $.when(
            $.ajax({
                type: 'POST',
                url: me.baseUrl + 'api/baskets/products/remove',
                headers: { 
                    'Content-type': 'application/json',
                    'Authorization': header 
                },
                data: JSON.stringify(productId)
            })
        ).then(
            function () {
                resolve();
            },
            function () {
                reject();
            }
        );
    })
}
Joram
  • 64
  • 7

1 Answers1

1

Why you don't store your basket locally? Then u could just update ur basket Object and let $scope do the rest?

Did u do sth. like that?:

Set a boolean variable $scope.basketHasChanged and change it when ur basket was changed successfully.

$watch that variable

scope.$watch('basketHasChanged', function(newValue, oldValue) {
   //and update your View here manually
});

I think this should be the easiest way.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
lolgans
  • 154
  • 1
  • 7
  • Yeah, that's what I'll do. I know Angular is a powerful framework. It just needs some help sometimes ($watch, $broadcast) ... – Joram May 04 '15 at 13:48