如何将 backbone 函数的结果传递给 jQuery $.when().then()?
how to pass the result of a backbone function into jQuery $.when().then()?
注意:我使用的是 jQuery 版本 1.7.2。
我正在尝试理解 jQuery 承诺和延迟对象。
我有以下代码:
var that = this;
var dataRetrieved = that.retrieveData();
$.when(dataRetrieved).then(function(data) {
that.formatDataForTemplate(data);
});
retrieveData: function () {
if ( // condition A ) {
return window.data.conditionA;
} else if (// condition B) {
return window.data.conditionB;
} else {
this.fetch({
success: function (status, response) {
return response;
}
});
}
}
基本上,我想将 return 从 retrieveData
编辑的任何数据传递给 .then()
函数,但它似乎不起作用。正在调用 retrieveData()
函数(使用 console.log 检查),但是未调用 formatDataForTemplate
。
retrieveData()
可能 return 即时数据,或者可能 return 来自 AJAX 查询的数据 (this.fetch({});
)。我需要 .then()
仅在数据从 retrieveData
编辑 return 后触发。
我想我只是没有清楚地理解承诺。我怎样才能让我的代码完成我想做的事情?
编辑:嗯,还是不太明白。这是我的更新版本。我正在尝试弄清楚如何 return 我的数据已解决的承诺。
var that = this;
var dataRetrieved = that.retrieveData();
dataRetrieved.then(function(data) {
that.formatDataForTemplate(data, that.get('template'));
});
retrieveData: function () {
var that = this;
if (window.settings.preview === 'true') {
return $.Deferred(function(def) {
def.resolveWith(window.settings.previewData);
});
}
// else if mock (during dev), use mock data.
else if (this.get('is_mock')) {
var mocked_data = {
"title":"Mock Title",
"description": "Mock Description"
};
// return mocked_data;
return $.Deferred(function(def) {
def.resolveWith(mocked_data);
});
}
// else, hit API like normal.
else {
return $.Deferred(function (def) {
that.fetch({
success: function (status, response) {
def.resolveWith(response);
}
});
});
}
},
为此,retrieveData
需要 return 一个用数据解决的承诺。
var that = this;
var dataRetrieved = that.retrieveData();
dataRetrieved.then(function(data) {
that.formatDataForTemplate(data);
});
retrieveData: function () {
if ( // condition A ) {
return window.data.conditionA; // this should be a promise that has been resolved with data
} else if (// condition B) {
return window.data.conditionB; // this should be a promise that has been resolved with data
} else {
var that = this;
return $.Deferred(function (def) {
that.fetch({
success: function (status, response) {
def.resolveWith(response);
}
});
});
}
}
注意:我使用的是 jQuery 版本 1.7.2。
我正在尝试理解 jQuery 承诺和延迟对象。
我有以下代码:
var that = this;
var dataRetrieved = that.retrieveData();
$.when(dataRetrieved).then(function(data) {
that.formatDataForTemplate(data);
});
retrieveData: function () {
if ( // condition A ) {
return window.data.conditionA;
} else if (// condition B) {
return window.data.conditionB;
} else {
this.fetch({
success: function (status, response) {
return response;
}
});
}
}
基本上,我想将 return 从 retrieveData
编辑的任何数据传递给 .then()
函数,但它似乎不起作用。正在调用 retrieveData()
函数(使用 console.log 检查),但是未调用 formatDataForTemplate
。
retrieveData()
可能 return 即时数据,或者可能 return 来自 AJAX 查询的数据 (this.fetch({});
)。我需要 .then()
仅在数据从 retrieveData
编辑 return 后触发。
我想我只是没有清楚地理解承诺。我怎样才能让我的代码完成我想做的事情?
编辑:嗯,还是不太明白。这是我的更新版本。我正在尝试弄清楚如何 return 我的数据已解决的承诺。
var that = this;
var dataRetrieved = that.retrieveData();
dataRetrieved.then(function(data) {
that.formatDataForTemplate(data, that.get('template'));
});
retrieveData: function () {
var that = this;
if (window.settings.preview === 'true') {
return $.Deferred(function(def) {
def.resolveWith(window.settings.previewData);
});
}
// else if mock (during dev), use mock data.
else if (this.get('is_mock')) {
var mocked_data = {
"title":"Mock Title",
"description": "Mock Description"
};
// return mocked_data;
return $.Deferred(function(def) {
def.resolveWith(mocked_data);
});
}
// else, hit API like normal.
else {
return $.Deferred(function (def) {
that.fetch({
success: function (status, response) {
def.resolveWith(response);
}
});
});
}
},
为此,retrieveData
需要 return 一个用数据解决的承诺。
var that = this;
var dataRetrieved = that.retrieveData();
dataRetrieved.then(function(data) {
that.formatDataForTemplate(data);
});
retrieveData: function () {
if ( // condition A ) {
return window.data.conditionA; // this should be a promise that has been resolved with data
} else if (// condition B) {
return window.data.conditionB; // this should be a promise that has been resolved with data
} else {
var that = this;
return $.Deferred(function (def) {
that.fetch({
success: function (status, response) {
def.resolveWith(response);
}
});
});
}
}