2013-02-21 2 views
0

fiddle은 내가하려고하는 일을 어느 정도 보여줍니다.질의 결과 개체를 별도의 함수로 전달

Parse.com 데이터베이스에서 일부 결과를 쿼리하고 있습니다. 결과는 success 콜백에서 반환됩니다.

누구나 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/