0
var app = angular.module('app',[]);

//in the html, the following 2 files are attached.
//          <script src="lib/sockjs-0.3.4.min.js"></script>
//          <script src="lib/vertxbus.min.js"></script>

app.factory('serverData',function(){
    var eb = new vertx.EventBus('http://xxx.xxx.xxx.xxx');
    var x = {};
    eb.send( "com.find.web.ed",{"Em":'[email protected]',"Pw":'123'}, 
        function(reply){
            x = reply;
        });
    var fact = {};
    fact.getData = function(){
        return x;
    };

    return fact;
});

app.controller('mainController',function($scope,serverData){

});

In the above code, i am trying to declare a factory to get data from a vertex server. It is not working, can some one help.?

It is working well when It is used in a controller. See the code.

var app = angular.module('app',[]);

app.controller('mainController',function($scope,$log){
$rootScope.user = {user :'[email protected]',password :'123'};
$rootScope.reply = {};

$scope.eb = new vertx.EventBus('http://100.100.100.100:8000');

$scope.loginFunction = function(){
            $scope.eb.send( "com.find.web.ed", 
                {"Em":$scope.user.user,"Pw":$scope.user.password},
                function(reply){
                 $rootScope.reply = reply;
                 $log.warn($rootScope.reply);
} 
);
}
});
2
  • Need more info on what's not working... Can you confirm you are able to execute the eb.send() method and populate x without using Angular? Assuming the event bus code works, are you hoping to call eb.send() every time serverData.getData() is called? Commented Feb 17, 2014 at 6:42
  • @AnthonyChu , I had updated the Question, Please see above. Commented Feb 17, 2014 at 7:10

2 Answers 2

1

Your factory code will only get the data once and it has no way of responding to the controller. I think you might be looking for something like this instead where send() is called every time and the controller provides a callback...

var app = angular.module('app',[]);

app.factory('serverData', function(){
    var eb = new vertx.EventBus('http://xxx.xxx.xxx.xxx');
    var fact = {};

    fact.getData = function(user, password, callback){
        // call send and pass it the callback function
        eb.send( "com.find.web.ed",
                 {"Em": user,"Pw": password}, 
                 callback
        );
    };

    return fact;
});

app.controller('mainController', function($scope, serverData){
    $scope.user = {user :'[email protected]',password :'123'};
    $scope.reply = {};

    serverData($scope.user.user, $scope.user.password, function (reply) {
        $scope.reply = reply;
        // might need $scope.$apply() here
    });
});

You can also do this via a $q promise...

var app = angular.module('app',[]);

app.factory('serverData', function($q){
    var eb = new vertx.EventBus('http://xxx.xxx.xxx.xxx');
    var fact = {};

    fact.getData = function(user, password){
        var deferred = $q.defer();
        // call send and pass it the callback function
        eb.send( "com.find.web.ed",
                 {"Em": user,"Pw": password}, 
                 function (reply) {
                     deferred.resolve(reply);
                 }
                 // also should reject on error
        );
        return deferred.promise;
    };

    return fact;
});

app.controller('mainController', function($scope, serverData){
    $scope.user = {user :'[email protected]',password :'123'};
    $scope.reply = {};

    serverData($scope.user.user, $scope.user.password)
        .then(function (reply) {
            $scope.reply = reply;
        });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Consider your following code

eb.send( "com.find.web.ed",{"Em":'[email protected]',"Pw":'123'}, 
        function(reply){
            x = reply;
        });

you are setting x with reply in callback. This is the problem. Your are replacing your entire object with reply object. And your fact.getData method pointing to old x, which is {}.

To solve create x in you fact object, or you can use promise as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.