我的工作簡單的流量+ Reactjs的應用程序,在我的商店,我有返回值:助焊劑:從阿賈克斯到組件
var ProfileStore = merge(EventEmitter.prototype, {
/**
* Get the entire collection of Profiles.
* @return {object}
*/
getAll: function() {
//ajax call to mongolab
var url = "https://api.mongolab.com/api/1/databases/bar/collections/profiles?apiKey=foo-"
var jsPromise = Promise.resolve($.ajax(url));
jsPromise.then(function(response) {
return response;
});
},
emitChange: function() {
this.emit(CHANGE_EVENT);
},
/**
* @param {function} callback
*/
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
/**
* @param {function} callback
*/
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
});
,然後在我的部分,我有:
var ProfileApp = React.createClass({
getInitialState: function() {
return {
allProfiles: ProfileStore.getAll()
};
},
});
當我在getAll()函數中調用console.log()時,我可以看到結果,但沒有任何內容被傳遞給我的組件,關於如何修復此問題的指針?
從您的店內進行異步呼叫並不總是最好的解決方案,因爲它會讓您更難以推斷您的狀態正在發生什麼,正如您已經注意到的那樣。我建議閱讀以下文章,其中描述了更好的方式來處理與flux的異步請求:http://www.code-experience.com/async-requests-with-react-js-and-flux-revisited/ – 2014-10-19 18:13:18