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:
01611922
Vues:
45
This message has been marked as a message which has helped to the initial question of the thread.
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.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform