0

尽管没有按时设置关系,但正在编写我的电子邮件对象(我自己的自定义类),任何想法如何正确链接它?

// 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('');
}

谢谢!贡

4

2 回答 2

2

因为与 Parse 的服务器对话是异步的,所以 Parse.Collection.create 使用 Backbone 样式的选项对象,并在创建对象时使用回调。我想你想做的是:

// 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('');
    }
  });
}
于 2012-07-13T19:18:24.767 回答
0

知道了!

this.emails 集合上的 .create 方法实际上并未返回对象,因此 var email 为空。Parse 以某种方式猜测它是 Email 类的一个空对象,所以我猜这个结构是 .create 完成它的工作后唯一剩下的东西。

相反,我使用 .query、.equalTo 和 .first 在服务器上检索电子邮件对象

// 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('');
}
于 2012-07-13T17:10:42.150 回答