2013-06-03 61 views
0

會喜歡這個幫助。我一直在嘗試很多不同的方法,並且發現這種方法非常複雜,並且認爲它可能更簡單,因爲我還沒有可用的解決方案。我已經問過Parse.com社區,但尚未回覆。如何搜索,分配和打印「孩子」對象類別/表格 - (Parse.com + Appcelerator)

我的數據存儲(parse.com)有兩個類/表。食物類型和食物項目。食物類型和食物項目之間存在一對多的關係。例如,Produce是類食物類型中的一個屬性(對象「名稱」),它有許多與其相關的食物項目(如蘋果,香蕉,生菜等)。這已經在數據存儲中正確構建。

在數據存儲中,食物類型有三個對象 - 顏色,foodItemId(關係)和名稱。名稱是指食物類型的名稱,例如Produce,Color只是我分配給name和foodItemId的一種顏色,點擊關係時,我現在添加了一堆水果進行測試。

食物項目類有四個對象/元素 - 過期,keyworkType(指它是一個水果,素食或其他),名稱和照片。這些都很自我解釋。

我想要做的是,當用戶想要添加一個項目到他們的庫存,他們選擇食物類型(例如生產)>它將他們帶到一個頁面打印出所有食物的食物類型(例如,查詢將檢索屬於特定食物類型的所有食物項目)。

所以我實際上試圖做的是匹配一個食物類型 - 「名稱」給用戶輸入的一個(在這種情況下它是產品)>然後檢索屬於該食物類型的所有食品。因此,我想要檢索食物類型爲「小孩」的所有食物項目對象。然後,能夠使用返回的食物項目來操縱和隨意使用任何我想要的東西。

總之,我嘗試過的一切都無法正常工作,我很樂意提供幫助。

謝謝你們,

基爾

回答

0

檢查......我想我已經找到了答案,它被證明是錯誤的。我已經找到了答案(這是你需要的代碼 - 這個參考對你有幫助!!! https://www.parse.com/questions/can-i-use-include-in-a-query-to-include-all-members-of-a-parserelation-error-102以及https://parse.com/docs/js/symbols/Parse.Relation.html):

//create an array that will hold all the unique KeywordsTypes in it. 
// This will be sent to another method to print out items that are in a KeywordType. 
var keyword_type_array = new Array(); 

//get the info out of the db and then set it in the variables 
var FoodType = Parse.Object.extend("Food_Type"); 
var FoodItem = Parse.Object.extend("Food_Item");  

//we want to find the Food Items that belong to a Food Type. 
var query = new Parse.Query(FoodType); 

// so make it so the inner query actually says find the matching food type 
query.equalTo("name", food_type); 
query.first({ 
    success: function(results) { 
     // get the results (object) and put them in a variable which will be used with relation 
     var innerQuery = results; 
     //make it a relational query.. aka.. find for the Food Type returned, the foodItemId and all that are in that corrospond to that field. 
     var relation = innerQuery.relation("foodItemId"); //https://www.parse.com/questions/can-i-use-include-in-a-query-to-include-all-members-of-a-parserelation-error-102 
     //run the relational query. 
     relation.query().find({ 
      success: function(theFoodItems) { 
       for (var i = 0; i < theFoodItems.length; i++) {    
        //add all keywordTypes to the keyword type array. We will go through and get unique types out. 
        keyword_type_array[i] = theFoodItems[i].get("keywordType");        
       } 

       // create a new array that will hold all unique keyword types. 
       var unique_keywords = new Array(); 

       // get unique keywords types. 
       for (var i = 0; i < keyword_type_array.length; i++) { 
        var unique = true; 

        for (var j = 0; j < unique_keywords.length; j++) { 
         if (keyword_type_array[i] == unique_keywords[j]) { 
          unique = false; 
          break; 
         }     
        } 

        if (unique) { 
         // if a keyword is found to be unique.. put that keyword in the array. 
         unique_keywords.push(keyword_type_array[i]);      
        } 
       } 

       // So now we know what we need to print, lets call a function to print them. 
       print_view_sections(unique_keywords); 

      }, 
      error: function(object, error) { 
       // The object was not retrieved successfully. 
       // error is a Parse.Error with an error code and description. 
       alert("Error: " + error.code + " " + error.message) 
      } 
     }); 
    }, 
    error: function(object, error) { 
     // The object was not retrieved successfully. 
     // error is a Parse.Error with an error code and description. 
     alert("Error: " + error.code + " " + error.message) 
    } 
});