2013-05-31 5 views
0

У меня есть объект с функцией. Когда я использую его таким образом, он всегда возвращает undefined. Как я могу заставить его возвращать все функции this.client[method].read(params).done?Возвращаемое значение из вложенной функции

rest.get('search', {query: 'Eminem', section: 'tracks'}) 

Вот объект:

var rest = { 

    // configuration 
    base: 'http://localhost/2.0/', 
    client: null, 

    get: function (method, params) { 

     // if client is null, create new rest client and attach to global 
     if (!this.client) { 
      this.client = new $.RestClient(this.base, { 
       cache: 5 //This will cache requests for 5 seconds 
      }); 
     } 

     // add new rest method 
     if (!this.client[method]) { 
      this.client.add(method); 
     } 

     // make request 
     this.client[method].read(params).done(function(response) { 
      //'client.foo.read' cached result has expired 
      //data is once again retrieved from the server 
      return response; 
     }); 
    } 
} 
+0

Что API это? Похоже, он использует какую-то систему Promise, которую вы могли бы использовать. –

+0

Этот код использует jquery-rest для работы с apis [link] (https://github.com/jpillora/jquery.rest) – Jurager

+0

Вы не можете, в этом вся причина использования обратных вызовов. –

ответ

3
get: function (method, params, callback) { 

    // if client is null, create new rest client and attach to global 
    if (!this.client) { 
     this.client = new $.RestClient(this.base, { 
      cache: 5 //This will cache requests for 5 seconds 
     }); 
    } 

    // add new rest method 
    if (!this.client[method]) { 
     this.client.add(method); 
    } 

    // make request 
    this.client[method].read(params).done(function(response) { 
     //'client.foo.read' cached result has expired 
     //data is once again retrieved from the server 
     callback(response); 
    }); 
    /* 
    simpler solution: 
    this.client[method].read(params).done(callback); 
    */ 
} 

Это асинхронный код, так что вы должны использовать обратные вызовы:

rest.get('search', {query: 'Eminem', section: 'tracks'}, function(response) { 
    // here you handle method's result 
}) 
2

Поскольку это, кажется, использует систему Promise, казалось бы, что вы можете просто вернуть результат .read(params), а затем позвонить .done() с обратным вызовом вместо cal вставьте его внутри .get().

var rest = { 
    // configuration 
    base: 'http://localhost/2.0/', 
    client: null, 

    get: function (method, params) { 

     // if client is null, create new rest client and attach to global 
     if (!this.client) { 
      this.client = new $.RestClient(this.base, { 
       cache: 5 //This will cache requests for 5 seconds 
      }); 
     } 
     // add new rest method 
     if (!this.client[method]) { 
      this.client.add(method); 
     } 

    // Just return the object 
     return this.client[method].read(params)); 
    } 
} 

rest.get('search', {query: 'Eminem', section: 'tracks'}) 
    .done(function(response) { 
     // use the response 
    }); 
Смежные вопросы