将 Parse.Query 检索到的对象分配给变量
Assigning object retrieved by Parse.Query to a variable
我正在尝试做一些看起来应该非常简单的事情,但我已经连续搜索了几个小时都没有成功。我只是想为给定的 objectId 查询 Parse.Object 的子类(理想情况下使用 .get() 但 .find 或 .first 也可以工作)并将整个对象分配给我可以在查询之外访问的变量。我知道查询 return 承诺并且我能够从我的 .then() 函数内部记录我想要捕获的对象,但我不知道如何在其他地方访问它。这是我现在所拥有的,我认为我在正确的轨道上。
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
console.log(results[0]);
});
这记录了我想要访问的整个对象,我可以在控制台中看到它的属性。但是,我希望能够在代码的其他地方访问 "thisLunch" 变量。就目前而言,我不能那样做。我假设这是因为变量的范围。我如何定义该变量以使其具有更大的范围
这是显示我正在尝试做的事情的更全面的和平代码
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
function assignLunch(results){
//not sure what goes here
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunch = thisLunchQuery.find().then(assignLunch);
var templateArgs ={
thisPlans: this.model.toJSON(),
thisLunch: thisLunch
};
$(this.el).html(this.template(templateArgs));
return this;
this.delegateEvents();
}
如您所见,我正在尝试获取对象,以便将其传递到模板并在那里访问其属性。我希望这会有所帮助,我对这一切都很陌生。
更新:
这是我尝试链接查询的最佳尝试,这样我就可以使用第一个查询的部分结果作为第二个查询的参数,然后在最终回调函数中使用两个查询对象。
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var thisLocationQuery = new Parse.Query(Location);
var lunchIdTemp = this.model.get("lunchId");
function assignLunch(results){
lunchObject = results[0];
thisLocationQuery.equalTo("objectId", results[0].get("locationId");
thisLocationQuery.find.then(assignLocation);
}
function assignLocation(locationResults, lunchObject){
// I'd like to be able to access the location I just found (locationResults[0]) as
//well as the original lunchId from the first query inside this callback
var templateArgs ={
thisPlans: this.model.toJSON(),
thisLunch: lunchObject,
thisLocation: locationResults[0];
};
$(this.el).html(this.template(templateArgs));
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunch = thisLunchQuery.find().then(assignLunch);
return this;
this.delegateEvents();
}
显然,这并不能安静地工作,但我不确定如何像我尝试的那样传递数据。也许我应该使用 .when?
您可以使用 thisLunch 作为参数调用函数,例如:
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
funcName(results[0])
});
或将其放入 global/window 对象中,例如:
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
window.myLunch = results[0];
});
您需要做的是定义一个函数来执行您想要对结果执行的操作,并将其传递给 promise success 方法。可能看起来像这样:
function doSomething(result) {
// do your stuff here
}
thisLunchQuery.find().then(doSomething)
更新:
在您的视图上下文中,代码可能类似于:
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
var self = this;
function assignLunch(results){
var templateArgs = {
thisPlans: this.model.toJSON(),
thisLunch: results
};
self.$el.html(self.template(templateArgs));
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunchQuery.find().then(assignLunch);
return this;
}
我正在尝试做一些看起来应该非常简单的事情,但我已经连续搜索了几个小时都没有成功。我只是想为给定的 objectId 查询 Parse.Object 的子类(理想情况下使用 .get() 但 .find 或 .first 也可以工作)并将整个对象分配给我可以在查询之外访问的变量。我知道查询 return 承诺并且我能够从我的 .then() 函数内部记录我想要捕获的对象,但我不知道如何在其他地方访问它。这是我现在所拥有的,我认为我在正确的轨道上。
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
console.log(results[0]);
});
这记录了我想要访问的整个对象,我可以在控制台中看到它的属性。但是,我希望能够在代码的其他地方访问 "thisLunch" 变量。就目前而言,我不能那样做。我假设这是因为变量的范围。我如何定义该变量以使其具有更大的范围
这是显示我正在尝试做的事情的更全面的和平代码
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
function assignLunch(results){
//not sure what goes here
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunch = thisLunchQuery.find().then(assignLunch);
var templateArgs ={
thisPlans: this.model.toJSON(),
thisLunch: thisLunch
};
$(this.el).html(this.template(templateArgs));
return this;
this.delegateEvents();
}
如您所见,我正在尝试获取对象,以便将其传递到模板并在那里访问其属性。我希望这会有所帮助,我对这一切都很陌生。
更新:
这是我尝试链接查询的最佳尝试,这样我就可以使用第一个查询的部分结果作为第二个查询的参数,然后在最终回调函数中使用两个查询对象。
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var thisLocationQuery = new Parse.Query(Location);
var lunchIdTemp = this.model.get("lunchId");
function assignLunch(results){
lunchObject = results[0];
thisLocationQuery.equalTo("objectId", results[0].get("locationId");
thisLocationQuery.find.then(assignLocation);
}
function assignLocation(locationResults, lunchObject){
// I'd like to be able to access the location I just found (locationResults[0]) as
//well as the original lunchId from the first query inside this callback
var templateArgs ={
thisPlans: this.model.toJSON(),
thisLunch: lunchObject,
thisLocation: locationResults[0];
};
$(this.el).html(this.template(templateArgs));
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunch = thisLunchQuery.find().then(assignLunch);
return this;
this.delegateEvents();
}
显然,这并不能安静地工作,但我不确定如何像我尝试的那样传递数据。也许我应该使用 .when?
您可以使用 thisLunch 作为参数调用函数,例如:
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
funcName(results[0])
});
或将其放入 global/window 对象中,例如:
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
thisLunchQuery.equalTo("objectId", lunchIdTemp);
var thisLunch = thisLunchQuery.find().then(function(results) {
window.myLunch = results[0];
});
您需要做的是定义一个函数来执行您想要对结果执行的操作,并将其传递给 promise success 方法。可能看起来像这样:
function doSomething(result) {
// do your stuff here
}
thisLunchQuery.find().then(doSomething)
更新:
在您的视图上下文中,代码可能类似于:
render: function() {
var thisLunchQuery = new Parse.Query(Lunch);
var lunchIdTemp = this.model.get("lunchId");
var self = this;
function assignLunch(results){
var templateArgs = {
thisPlans: this.model.toJSON(),
thisLunch: results
};
self.$el.html(self.template(templateArgs));
}
thisLunchQuery.equalTo("objectId", lunchIdTemp);
thisLunchQuery.find().then(assignLunch);
return this;
}