Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
angular.module('SomeApp').controller('userListsCtrl',userListsCtrl);

function userListsCtrl(DTOptionsBuilder,DTColumnBuilder,$http,$q) {
    var vm = this;
    function serverData() {
        var defer = $q.defer();
        $.ajax({
            'dataType': 'json',
            'type': 'POST',
            'contentType': 'application/json',
            'url': 'cgi/userNavLists.py',
            'data': JSON.stringify({'request':"INPROCESS"}),
            'success': function(data, textStatus, jqXHR, fnCallback){
                console.log("User Table Data Coming Up");
                console.log(data);
                defer.resolve(data);
            },

        });

        return defer.promise;
    }

   serverData().then(function(data){
        console.log("Promise Data");
        console.log(data);
   });
    vm.dtOptions = DTOptionsBuilder.fromSource('cgi/userNavLists.py').withFnServerData(serverData);
                console.log("User Table Data Coming Up123");
    console.log(vm.dtOptions);
    vm.dtColumns = [
        DTColumnBuilder.newColumn('requestId').withTitle('ID'),
        DTColumnBuilder.newColumn('requestType').withTitle('Type'),
        DTColumnBuilder.newColumn('identifier').withTitle('Identifier'),
        DTColumnBuilder.newColumn('domainName').withTitle('Domain Name'),
        DTColumnBuilder.newColumn('recordType').withTitle('Record Type'),
        DTColumnBuilder.newColumn('state').withTitle('State'),
    ];


}

I am trying to render table fetching data from server. But my table is getting render before data is being fetched.I used promise to do the job but seems it ain't fulfilling my problem. Is their a way I can make it work???

share|improve this question
    
Is there any reason you're using $.ajax and not $http? I don't think $aja is needed –  dcodesmith May 15 at 10:25
    
I am comfortable using $.ajax, so...........otherwise no grudge using $http –  Pravin Agre May 15 at 10:27
    
Ok, I would have into sticking the angular way but .... Anyway, you can get rid of the $http reference in your factory ;) –  dcodesmith May 15 at 10:28

1 Answer 1

You have to create your object together with the asynchronous response:

angular.module('SomeApp').controller('userListsCtrl',userListsCtrl);

function userListsCtrl(DTOptionsBuilder,DTColumnBuilder,$http,$q) {
    var vm = this;
    function serverData() {
        var defer = $q.defer();
        $.ajax({
            'dataType': 'json',
            'type': 'POST',
            'contentType': 'application/json',
            'url': 'cgi/userNavLists.py',
            'data': JSON.stringify({'request':"INPROCESS"}),
            'success': function(data, textStatus, jqXHR, fnCallback){
                console.log("User Table Data Coming Up");
                console.log(data);
                defer.resolve(data);
            },

        });

        return defer.promise;
    }

   serverData().then(function(data){
        console.log("Promise Data");
        console.log(data);
    vm.dtOptions = DTOptionsBuilder.fromSource('cgi/userNavLists.py').withFnServerData(serverData);
                console.log("User Table Data Coming Up123");
    console.log(vm.dtOptions);
    vm.dtColumns = [
        DTColumnBuilder.newColumn('requestId').withTitle('ID'),
        DTColumnBuilder.newColumn('requestType').withTitle('Type'),
        DTColumnBuilder.newColumn('identifier').withTitle('Identifier'),
        DTColumnBuilder.newColumn('domainName').withTitle('Domain Name'),
        DTColumnBuilder.newColumn('recordType').withTitle('Record Type'),
        DTColumnBuilder.newColumn('state').withTitle('State')
    ];
   });
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.