gpt4 book ai didi

javascript - parse.com 将其绑定(bind)到成功 block

转载 作者:行者123 更新时间:2023-12-03 10:17:05 24 4
gpt4 key购买 nike

我有一个简单的嵌套查询,但我不知道如何将“this”变量绑定(bind)到成功 block 。我添加了几行额外的代码来提供更多上下文。

  initialize: function() {

_.bindAll(this, 'addAll', 'addOne', 'logOut', 'newJob', 'showDetail');

this.$el.html(_.template($("#manage-jobs-template").html()));

this.jobs = new JobList;
this.requests = new RequestList;

this.jobs.query = new Parse.Query(Job);
this.jobs.query.equalTo('jobPostedBy', Parse.User.current());
this.jobs.fetch({

success: function (jobs) {
//This is undefined here.
this.requests.query = new Parse.Query(Request);
this.requests.query.containsAll("job", jobs);
this.requests.fetch();
}

});

最佳答案

一种可能的解决方案是在 initialize 函数中保存对 this 的引用,然后在 success 函数中使用它。 self 将在 success 中可用:

initialize: function(){
var self = this;
...
this.jobs.fetch({
success: function(jobs){
self.request.query = ... //--- here self references the "outer" this
....
}
});
}

另一种解决方案是使用 bind() 将上下文绑定(bind)到匿名函数:

initialize: function(){
...
this.jobs.fetch({
success: (function(jobs){
this.request.query = ...
....
}).bind(this); //--- context binding
});
}

看看这个例子:https://jsfiddle.net/8e8yfxx9/3/

关于javascript - parse.com 将其绑定(bind)到成功 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29822849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com