Skip to main content

Angularjs function calls order

I have a dropdown list of options, which are shown or not depending on conditions.

   

Here is my controller:

function FormOrderDialogController(tsModulesService, $scope, $q, $http, $uibModalInstance, params, $filter, Requester)                              

    Requester.restGet("/events/" + params.eventId, null, params.serverId).then((data)=>{
        vm.event = data;
    });

    Requester.restGet('/dic/10', null, null, null, true).then((resp) => {
        vm.templateTypes = resp;
        vm.templateType = vm.templateTypes[0].id;
    });    

     vm.isShowableTemplate = isShowableTemplate;

     function isShowableTemplate(templateType) {

        switch (templateType.id) {
            case 321:
                return !!vm.event.info.ticketTemplate;
            case 322:
                return !!vm.event.info.ticketETemplate;
        }
    }                                                                           

By the time isShowableTemplate is called I expect event object to be filled, And the thing is the getEvent function is called twice, once before isShowableTemplate get called, and once after. The problem is event is undefined after the first call and I get an error "Cannot read property 'info' of undefined". My question is why is it so and what I am doing wrong. I am new to both js and angular, so may be I miss something essential.

Solved

Why not remove the function and filter:

function FormOrderDialogController(tsModulesService, $scope, $q, $http, $uibModalInstance, params, $filter, Requester)                              

    Requester.restGet("/events/" + params.eventId, null, params.serverId)
        .then(data => {
            vm.event = data;

            return Requester.restGet('/dic/10', null, null, null, true);
        })
        .then(resp => {
            vm.templateTypes = resp;
            vm.showableTemplateTypes = resp.filter(t => { 
                switch (t.id) {
                    case 321:
                        return !!vm.event.info.ticketTemplate;
                    case 322:
                        return !!vm.event.info.ticketTemplate;
                }

                return false; // or true depending if you want to show the     others.
            });

            vm.templateType = vm.templateTypes[0].id;
        });   
}  

I've combined the two Promises together because you use the vm.event in the second response. By doing it this way, you can always guarantee some value for vm.event.

The html:


Comments

Popular posts from this blog

Can't deploy apk from eclipse in ubuntu 10.10

I've been making some apps for android for quite a while but I've only been using windows 7. Due to some reasons sometimes I'm forced to program in linux so I followed the guide to configure the SDK and eclipse (http://developer.android.com/guide/developing/device.html). After that I ran "adb devices" and got this: List of devices attached ???????????? device As you can see the name is unknown but this also happens in windows so I didn't fret too much over it. Then I launched eclipse and made a very simple project to test the deployment but here two things happened: 1st- It doesn't detect the android SDK version. (http://img515.imageshack.us/img515/5611/escolha.jpg) 2nd- when I try to deploy I get the following output: [2010-10-09 23:06:45 - testeAndroid] Android Launch! [2010-10-09 23:06:45 - testeAndroid] adb is running normally. [2010-10-09 23:06:45 - testeAndroid] Performing com.examples.teste.teste activity launch [2010-10-09 23:06:45...

os.getenv("xxx") shows different result while using sudo or not to run python3

It happened to me when I try to use os.getenv. I have edited the "\etc\profile" file to add an env variable and ran the source command. And I wrote some codes to test it.It worked well and I can get the env variable I set when I run python just using python3 command in the command line. But I find it doesn't work when I using sudo python3.The os.getenv("xxx") returns None. That's the question.Why it doesn't work just because of using sudo. Solved sudo does not keep the user's environment variables. Maybe this can help: How to keep Environment Variables when Using SUDO