Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to avoid calling this method twice?
Message
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01611738
Message ID:
01611923
Vues:
25
I see the difference now, thanks. In your code you didn't have variables as we do, you have function without variable declaration.

I'll make these adjustments.

Very good blog, BTW, thanks a lot for the reference.

>If you assigning the function to a variable it won't be hoisted and so wouldn't be visible. So if you changed the code then that wouldn't have worked.
>
>+++ Rick ---
>
>>>Huh? What browser are you using???
>>>
>>>Function hoisting will always push function (and variable) declarations to the top of the scope. So the functions are visible before they are declared.
>>>
>>>http://designpepper.com/blog/drips/variable-and-function-hoisting
>>>
>>>I use this reveling module pattern in just about all my javascript code and it definitely works including for Angular modules.
>>>
>>>+++ Rick ---
>>>
>>I am using the latest Google Chrome browser. We're using v1.2.7 of AngularJs. I know that when I declared service variable on top and functions right after that, I got functions undefined error. This version of the service works now and only makes a single call:
>>
>>
>>(function () {
>>    'use strict';
>>    
>>    var app = angular.module('sysMgrApp');
>>
>>    app.factory('invoicesService', ['$http', '$q', function($http, $q) {
>>
>>        var deferredMeta = null;
>>
>>        var invokeCallNumber = 0;
>>        // Implementation
>>        var getMetaData = function () {
>>            
>>            if (deferredMeta) {
>>                return deferredMeta.promise;
>>            }
>>
>>            deferredMeta = $q.defer();
>>
>>            if (service.metaData)
>>                deferredMeta.resolve(service.metaData);
>>            else {
>>                
>>                $http.get('/api/invoices/metadata')
>>                    .success(function (data) {
>>                        invokeCallNumber++;
>>                        window.console && console.log('Calling http service ' + invokeCallNumber + ' time...')
>>                        service.metaData = data;
>>                        deferredMeta.resolve(data);
>>                    })
>>                    .error(function (data, status, header, config) {
>>
>>                        deferredMeta.reject(status);
>>                    });
>>            }
>>
>>            return deferredMeta.promise;
>>        };
>>
>>        var getInvoices = function (queryRequest) {
>>            var deferred = $q.defer();
>>            $http.get('/api/invoices', { params: queryRequest })
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var getAccountInvoices = function (queryRequest) {
>>            var deferred = $q.defer();
>>            $http.get('/api/invoices/getAccountInvoices', { params: queryRequest })
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var getNonFinalizedInvoices = function(queryRequest) {
>>            var deferred = $q.defer();
>>            $http.get('/api/invoices/getInvoicesToApplyFees', { params: queryRequest })
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var processPayments = function (transactionObject) {
>>            var deferred = $q.defer();
>>            $http.post('/api/invoices/processPayments', transactionObject)
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var getAccountInvoices = function (queryRequest) {
>>            var deferred = $q.defer();
>>            $http.get('/api/invoices/getAccountInvoices', { params: queryRequest })
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var getInvoicesToBePaid = function (queryRequest) {
>>            var deferred = $q.defer();
>>            $http.get('/api/invoices/getInvoicesToBePaid', { params: queryRequest })
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var getInvoice = function (id) {
>>            var deferred = $q.defer();
>>            $http.get('/api/invoices/' + id)
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var createInvoice = function (invoice) {
>>            var deferred = $q.defer();
>>            $http.post('/api/invoices', invoice)
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var saveInvoice = function (invoice) {
>>            var deferred = $q.defer();
>>            $http.put('/api/invoices', invoice)
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(data, status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        var deleteInvoice = function (invoiceNo) {
>>            var deferred = $q.defer();
>>            $http.delete('/api/invoices/' + invoiceNo)
>>                .success(function (data) {
>>                    deferred.resolve(data);
>>                })
>>                .error(function (data, status, header, config) {
>>                    deferred.reject(status);
>>                });
>>            return deferred.promise;
>>        };
>>
>>        // Interface
>>        var service = {
>>            metaData: null,
>>            getMetaData: getMetaData,
>>            getInvoices: getInvoices,
>>            getAccountInvoices: getAccountInvoices,
>>            getInvoicesToBePaid: getInvoicesToBePaid,
>>            getInvoice: getInvoice,
>>            createInvoice: createInvoice,
>>            saveInvoice: saveInvoice,
>>            deleteInvoice: deleteInvoice,
>>            processPayments: processPayments
>>        };
>>
>>        return service;
>>    }]);
>>})();
>>
>>
>>BTW, it's good thing I posted as I see that I forgot to declare a new function I just added.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform