jQuery:理解.done()和.fail()

时间:2015-07-01 20:42:36

标签: javascript jquery parse-platform

我正在跳进另一位开发人员撰写的项目,并试图绕过代码库。

以下是我遇到问题的代码:

var ret;
ret = new $.Deferred();

new Parse.Query(Model).include('companyDetails').matchesQuery('companyDetails', 
new Parse.Query(UserModel).equalTo('objectId', 'seppWfGi20')).first().done((function(_this) {
  return function(person) {
    if (person) {
      _this.data.person = person;
      return ret.resolve(_this.data.person);
    } else {
      return ret.reject('person');
    }
  };
})(this)).fail((function(_this) {
  return function(error) {
    return ret.reject('person');
  };
})(this));

ret;

我无法确定.done().fail()方法正在做什么。另外,那里的.first()是什么?

任何jQuery忍者都能帮助解释一下这段代码的作用吗?

感谢任何帮助。提前谢谢!

1 个答案:

答案 0 :(得分:0)

这些是form of promises。在这种情况下,它允许在成功/失败发生时调用显式函数。它相当于......

Parse.query({query here}, {
    success: function(response){}, // this is the done function
    error: function(response){} // this is the fail function
})

最后,.first()是特定于Parse的,告诉它获取查询集中的第一个结果。我说你最好解开那些mess of IIFE陈述。

相关问题