我有一个控制器这将启动在工厂API调用。目前,我开始呼叫,然后我有调用后函数来检查阵列
的状态。但是我不知道如何迫使它在这里等待数据被收集,因此需要某种 $ Q
实施
I have a Controller which starts an API call in a Factory. Currently I start the call, then I have a function after that call to check the status of the Array
. However I'm not sure how to force it to wait here while the data is being gathered, thus the need for some kind of $q
implementation.
正如你可以在下面的截图中看到, vs.tickers
返回一个空对象或数组。后来终于在的console.log
在GetTickersFactory火灾:
As you can see in the screenshot below, vs.tickers
returns an empty Object, or Array. Then finally the console.log
in GetTickersFactory fires:
第一控制器
if (root.tickerType === 'portfolio') {
// Call to factory to start the API GETS:
GetTickersFactory.getTickers('portfolio');
// I then call a function in the Factory to return the array
// Which isn't ready yet of course since it returns undefined...
vs.tickers = GetTickersFactory.returnPortfolioTickers();
console.log('portfolio');
console.log('vs.tickers = ', vs.tickers);
}
在GetTickersFactory getTickers功能 |也许这可以帮助:主旨为全厂
getTickers function in the GetTickersFactory | Perhaps this helps: the Gist for the full Factory.
function getTickers(type, load, searchedTicker) {
load = load || loadString; searchedTicker = searchedTicker || '';
var portfolioTickersArray = [], searchedTickersArray = [];
tickersPane.loadingTickersDone = false;
switch(type) {
case 'searched':
....
break;
case 'portfolio':
if (portfolioCached) {
// The API Call (cached)
ApiFactory.getWatchList().then(function(data) {
portfolioTickersArray = renderTickers(data.data.tickers, undefined, type);
portfolioTickers.that = portfolioTickersArray;
tickersPane.loadingTickersDone = true;
console.log('portfolioTickersArray: ', portfolioTickersArray);
return portfolioTickersArray;
});
} else {
// The API Call (not cached)
ApiFactory.getWatchListRefresh().then(function(data) {
portfolioTickersArray = renderTickers(data.data.tickers, undefined, type);
portfolioTickers.that = portfolioTickersArray;
portfolioCached = true;
tickersPane.loadingTickersDone = true;
console.log('portfolioTickersArray: ', portfolioTickersArray);
return portfolioTickersArray;
});
}
break;
}
function renderTickers(data, searchedTicker, type) {
....
}
}
我使用,我相信我不应该使用,无论这一点,并找出如何使用的承诺,而不是getTickersFactory.js内返回数组功能:
function returnPortfolioTickers() {
return portfolioTickers.that;
}
请注意我本来试试这个,但具有相同的结果:
Note I did originally try this, but with the same results:
vs.tickers = GetTickersFactory.getTickers('portfolio');
vs.tickers
将返回未定义
您开关的情况下应该返回的承诺,以便它调用函数将。然后
调用时的承诺得到解决
Your switch cases should return a promise so that it caller function will .then
called when promise gets resolved
厂code
function getTickers(type, load, searchedTicker) {
//other code remains same
tickersPane.loadingTickersDone = false;
switch (type) {
case 'searched':
return ApiFactory.getTickers(null, load).then(function(data) {
//other code remains same
if (tickersPane.tempTickers.length > 0) {
//other code remains same
return returnData(searchedTickersArray);
}
return []; //return default variable to continue promise chain
});
break;
case 'portfolio':
tickersPane.addOption = false;
tickersPane.removeOption = true;
tickersPane.displayTopTags = false;
tickersPane.displayPortfolio = true;
if (portfolioCached) {
return ApiFactory.getWatchList().then(function(data) {
//other code remains same
return returnData(portfolioTickersArray);
});
} else {
return ApiFactory.getWatchListRefresh().then(function(data) {
//other code remains same
return returnData(portfolioTickersArray);
});
}
break;
}
function renderTickers(data, searchedTicker, type) {
//this should be as is
}
function returnData(data) {
tickersPane.loadingTickersDone = true;
return data;
}
//tickersPane.loadingTickersDone = true;
//return data; //removed this line and move to function
}
控制器
if (root.tickerType === 'portfolio') {
// Call to factory to start the API GETS:
GetTickersFactory.getTickers('portfolio').then(resp){
vs.tickers = GetTickersFactory.returnPortfolioTickers();
console.log('portfolio');
console.log('vs.tickers = ', vs.tickers);
};
}