0

I structured my code with requireJS. In my init.js

require(["jquery", "user"], function($, user){
    console.log(user.isLogin());
});

In my user.js

define(["jquery"], function($){
    return{
        isLogin: function(){
             var status = 0;
             function callApi(){
                 return $.ajax({
                 type: "post",
                 url: urls.api_isLogin,
                 dataType: "json"
             }
             callApi().done(function(data){
                 status = data.data;
                 console.log(status);
                 return status;
             });    
       }
    };
});

I can get the data from ajax call:

console.log(status);
//output: true
//means I can get the value from the ajax call

But how can I pass it into init.js?
I got "undefined" from:

console.log(user.isLogin());
SPG
  • 5,691
  • 11
  • 42
  • 72
  • 2
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Louis May 28 '15 at 10:39
  • I am asking how to implement this with requireJS, please read carefully. – SPG May 29 '15 at 02:21

1 Answers1

0

OK. I figured out by myself.

define(["jquery"], function($){
    return{
        isLogin: function(){
                 return $.ajax({
                 type: "post",
                 url: urls.api_isLogin,
                 dataType: "json"
             }    
       }
    };
});

Then in another file:

$.when(user.isLogin()).then(function(data){
    console.log(data.data);
});
SPG
  • 5,691
  • 11
  • 42
  • 72