2013-02-21 94 views
0

這個fiddle或多或少地顯示了我正在嘗試執行的操作。將查詢結果對象傳遞給單獨的函數

我在查詢Parse.com數據庫的一些結果。結果將在成功回調中返回。

任何人都可以告訴我在mainView()函數中使用它們的最佳方法嗎?我希望將所有查詢與它們的顯示邏輯分開。我嘗試了很多不同的方法,但一直未能實現。

回答

2

只需在回調之外存儲對this的引用即可。

var userInterface = { 


    newQuery: function() { 
     var that=this; //store a reference to "this" 

     Query = Parse.Object.extend("Test"); 
     query = new Parse.Query(Query); 

     query.descending("createdAt");  
     query.equalTo("column1", "a"); 

     query.find({ 
      success:function(results){ 

       that.mainView(results); //that points to the userInterface object 

      }, 
      error:function(error){ 

      } 


     }); 
    }, 



    mainView: function(res){ 

     console.log(res); 

    }, 

    init: function() { 
     this.newQuery();   
    } 

}; 
userInterface.init(); 

http://jsfiddle.net/4XsLq/8/