2014-03-25 45 views
1

我想實現對象生命週期的歷史功能。基本上我有一個與歷史有一對多關係的對象。含義查詢對象具有指向歷史行數組的指針。當一個創建這個對象之前保存我這樣做解析對象的歷史實現

Parse.Cloud.beforeSave("Enquiry", function(request, response) 
    var currentUser = request.user; 
    var currentObject = request.object; 

    if (!currentObject.id) { 
      var historicalData = new Array(); 

      var history = new Parse.Object("History"); 
      var userName = currentUser.get("firstname") + " " + currentUser.get("lastname"); 
      history.set("user", userName); 
      history.set("summary", "Enquiry created "); 
      history.save(); 
      historicalData.push(history); 

      currentObject.set("history", historicalData); 
    } else { 
      //code to fetch existing Enquiry and figure out what changed 
      ... 
      ... 


      var historicalData = existingEnquiry.get("history"); 

      var history = new Parse.Object("History"); 
      history.set("user", userName); 
      history.set("summary", summary); 
      history.save(); 
      historicalData.push(history); 
      currentObject.set("history", historicalData); 
    } 
} 

現在我可以看到,歷史對象被創建並分配到新創建的對象罰款。另外,當我更新對象,我可以看到另一個歷史對象被保存在分貝,但問題是,查詢歷史引用不會改變,這意味着查詢對象仍然指向創建查詢時創建的歷史對象。

我不明白的是,即使我已經將更新的數組添加到了查詢的歷史記錄列,爲什麼該列沒有更新。任何幫助,將不勝感激。

回答

0

您必須在「currentObject.set(」history「,historicalData)」語句之後保存當前對象。

即 currentObject.save();

+0

試過,沒有幫助 –

+0

也請看到方法在save之前,這意味着保存會在這之後被調用。 –

+0

嘗試添加response.success();最後。可能是解析不接受響應,因此不會在afterSave之後調用。 – Sandeep