0

私のEメールオブジェクト(私自身のカスタムクラス)は、関係が時間どおりに設定されていませんが、書き込まれています。これを適切にチェーンする方法はありますか?

// 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メソッドは実際にはオブジェクトを返さないため、varemailは空でした。どういうわけか、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 に答える