Tim Penner
Tim Penner

Reputation: 3541

Calling $wakanda.init multiple times from different Angular controllers

I am developing a mobile application built in Wakanda Digital App Factory 1.0.3 using Ionic and AngularJS with a 4D backend database.

I have two different 4D methods available through 4D-Mobile via two separate 4D tables that are accessed via two different Angular controllers:

.controller('homeCtrl', function($scope, $wakanda) {
    $wakanda.init('servers').then(function(ds) {
        ds.servers.www4DMionicHomeOverview().$promise.then(function(event) {
            $json = event.result;
            $scope.overview = $json.servers;
            $scope.healthCheck = $json.healthCheck;
        }, function(err) {
            debugger
            console.log(err);
        });
    }, function(err) {
        debugger
        console.log(err);
    });
})

.controller('errorLogCtrl', function($scope, $wakanda) {
    $wakanda.init('server_log').then(function(ds) {
        ds.server_log.www4DMionicErrorLog().$promise.then(function(event) {
            $json = event.result;
            $scope.errors = $json;
        }, function(e) {
            debugger
            console.log(e);
        });
    }, function(e) {
        debugger
        console.log(e);
    });
})

I am noticing a strange issue calling these 4D methods in that the first one will work but the second one will fail, regardless of which one i call first. That is, if i call ds.server_log.www4DMionicErrorLog() first it works but then subsequent calls to ds.servers.www4DMionicHomeOverview() fail until i refresh the browser.

The opposite is also true in that if i call ds.servers.www4DMionicHomeOverview() first then it works but subsequent calls to ds.server_log.www4DMionicErrorLog() fail.


The error i get for the second method is:

ionic.bundle.js:25642 TypeError: Cannot read property 'www4DMionicHomeOverview' of undefined

or

ionic.bundle.js:25642 TypeError: Cannot read property 'www4DMionicErrorLog' of undefined

Depending on which of the two methods I call first.


I am curious if this may be related to how i am calling $wakanda.init from each controller. The documentation does not say it is bad to do this.

Would it be better to setup an Angular service and resolve $wakanda in the service?

Upvotes: 1

Views: 160

Answers (2)

David Ringsmuth
David Ringsmuth

Reputation: 335

My project uses 1.3.7 AngularJS

In an attempt to be more efficient, I reduce the number of calls to

$wakanda.init().then(function oninit(ds) {
                            $rootScope.ds = $wakanda.$ds;

Future calls use $rootScope.ds.

However each of my Services has it's own call to $wakanda.init();

        userCredentials: function(login,password,validate) {
        var deferred = $q.defer();

        $wakanda.init().then(function oninit(ds) {
            ds.User.userCredentials(login,password,validate, {
                onSuccess: function(data) {
                    deferred.resolve(data);
                },
                onError: function(err) {
                    deferred.reject(err.message);
                }
            });
        }); //$wakanda.init()

        return deferred.promise;
    },

I found that within the UI controllers multiple calls to $wakanda.init() would fail.

Upvotes: 1

Stéphane Garnier
Stéphane Garnier

Reputation: 199

Replace

 $wakanda.init('servers').then(function(ds) {...

by

$wakanda.init().then(function(ds) {...

And do the same for all in your controllers.

In your case, I recommend you to use $wakanda.init()

Upvotes: 2

Related Questions