2012-07-13 154 views
0

雖然關係沒有按時設置,但我的電子郵件對象(我自己的自定義類)正在編寫,有關如何正確鏈接它的任何想法?在對象創建時/創建對象後設置Parse.Object.relation

// Create new Email model and friend it 
addFriendOnEnter: function(e) { 
    var self = this; 
    if (e.keyCode != 13) return; 

    var email = this.emails.create({ 
    email: this.emailInput.val(), 
    ACL:  new Parse.ACL(Parse.User.current()) 
    }); 

    var user = Parse.User.current(); 
    var relation = user.relation("friend"); 
    relation.add(email); 
    user.save(); 

    this.emailInput.val(''); 
} 

謝謝! Gon

回答

2

因爲說話解析的服務器是異步的,Parse.Collection.create使用骨幹風格的選擇與對象創建對象時的回調。我認爲你想要做的是:

// Create new Email model and friend it 
addFriendOnEnter: function(e) { 
    var self = this; 
    if (e.keyCode != 13) return; 

    this.emails.create({ 
    email: this.emailInput.val(), 
    ACL:  new Parse.ACL(Parse.User.current()) 
    }, { 
    success: function(email) { 
     var user = Parse.User.current(); 
     var relation = user.relation("friend"); 
     relation.add(email); 
     user.save(); 

     self.emailInput.val(''); 
    } 
    }); 
} 
+0

Thanks @bklimt!事實上,新的骨幹,聽起來像'成功'總是一個[選項] :)甜! – 2012-07-13 20:58:41

0

Got it!

this.emails集合上的.create方法實際上並不返回對象,所以var email是空的。不知何故,Parse猜測它是一個類Email的空對象,所以我猜結構是唯一一次保留.create完成它的工作。

相反,我使用.query,.equalTo檢索服務器上的電子郵件和對象。首先

// Create new Email model and friend it 
addFriendOnEnter: function(e) { 
    var self = this; 
    if (e.keyCode != 13) return; 

    this.emails.create({ 
    email: this.emailInput.val(), 
    ACL:  new Parse.ACL(Parse.User.current()) 
    }); 

    var query = new Parse.Query(Email); 
    query.equalTo("email", this.emailInput.val()); 
    query.first({ 
    success: function(result) { 
     alert("Successfully retrieved an email."); 
     var user = Parse.User.current(); 
     var relation = user.relation("friend"); 
     relation.add(result); 
     user.save(); 
    }, 
    error: function(error) { 
     alert("Error: " + error.code + " " + error.message); 
    } 
    }); 

    this.emailInput.val(''); 
}