In the primeData function within the datacontext, four queries are made to a back-end Web API service:
function primeData(forceRefresh) {
return (getLookups(forceRefresh) // this needs to complete before moving on
.then(success))
.then(model.initialize(datacontext)) // depends on getLookups
.then(getDatasetOne()) // depends on model.initialize
.then(getDataSetTwo()) // depends on model.initialize
.then(getNonDependantDataset); // doesn't depend on previous calls
function success(data) {
if (!initialized) {
datacontext.lookups = data.results[0];
initialized = true;
} else {
datacontext.lookups = {
teams: getLocal('Teams', 'id'),
// other lookup values here
};
}
// should I call model.initialize(datacontext) here??
}
}
The second and third queries (getDatasetOne() and getDatasetTwo()) rely on model.initialize, which in turn relies on the results of the first query (getLookups()). How can we ensure that model.initialize is not executed until getLookups has successfully completed, and also make sure that getDatasetOne/Two are only called after the model has been initialized. The code has been simplified for clarity, but there seems to be confusion related to Q library usage. Any guidance on this issue would be appreciated.
CONTROLLER:
'use strict';
app.controller('HomeController',
['$scope', 'breeze', 'datacontext', 'logger',
function HomeController($scope, breeze, datacontext, logger) {
function initialize(forceRefresh) {
datacontext.initialize(forceRefresh)
.then(getSucceeded)
.fail(queryFailed)
.fin(refreshView);
}
initialize();
$scope.refresh = refresh;
function refresh() {
initialize(true);
}
function getSucceeded() {
$scope.lookups = datacontext.lookups;
$scope.datasetOne = datacontext.datasetOne;
$scope.datasetTwo = datacontext.datasetTwo;
}
function refreshView() {
$scope.$apply();
}
function queryFailed(error) {
logger.error(error);
}
}]);
DATACONTEXT:
app.factory('datacontext',
['breeze', 'Q', 'logger', 'model',
function (breeze, q, logger, model) {
var initialized;
var manager = configureBreezeManager();
var datacontext = {
initialize: initialize,
metadataStore: manager.metadataStore,
saveEntity: saveEntity
};
return datacontext;
//#region private members
function initialize(forceRefresh) {
if (!initialized || forceRefresh) {
return primeData(forceRefresh).then(function () {
logger.log("Running Initialize");
});
} else {
logger.log("Already Initialized");
return q();
}
}
function primeData(forceRefresh) {
return (getLookups(forceRefresh)
.then(success))
.then(model.initialize(datacontext))
.then(getDatasetOne())
.then(getDataSetTwo())
.then(getNonDependantDataset);
function success(data) {
if (!initialized) {
datacontext.lookups = data.results[0];
initialized = true;
} else {
datacontext.lookups = {
teams: getLocal('Teams', 'id'),
// other lookup values here
};
}
// should I call model.initialize(datacontext) here??
}
}
function getLookups(forceRefresh) {
var query = breeze.EntityQuery
.from('Lookups');
if (initialized && !forceRefresh) {
return true;
}
return manager.executeQuery(query);
}
function getDatasetOne() {
var query = breeze.EntityQuery
.from("EntityNameOne");
return manager.executeQuery(query).then(getSucceeded);
function getSucceeded(data) {
datacontext.datasetOne = model.process(data.results)
return q();
}
}
function getDatasetTwo() {
var query = breeze.EntityQuery
.from("EntityNameTwo");
return manager.executeQuery(query).then(getSucceeded);
function getSucceeded(data) {
datacontext.datasetTwo = model.process(data.results);
return q();
}
}
function getNonDependentDataset() {
var query = breeze.EntityQuery
.from('EntityNameThree');
return manager.executeQuery(query).then(success);
function success(data) {
datacontext.nonNependentDataset = data.results;
}
}
function saveEntity(masterEntity, message) {
// standard implementation
}
function getLocal(resource, ordering) {
var query = breeze.EntityQuery.from(resource)
.orderBy(ordering);
return manager.executeQueryLocally(query);
}
function configureBreezeManager() {
// standard implementation
}
}]);
MODEL:
app.factory('model', ['logger', function (logger) {
var datacontext;
extendDatasetOne();
extendDatasetTwo();
var model = {
initialize: initialize,
getDatasetOne: getDatasetOne,
getDatasetTwo: getDatasetTwo
};
return model;
function initialize(context) {
datacontext = context;
}
function getDatasetOne(input) {
var ret = [];
for (var i = 0; i < input.length; i++) {
var item = new TypeOne(input[i]);
ret.push(item);
}
return ret;
}
function getDatasetTwo(input) {
var ret = [];
for (var i = 0; i < input.length; i++) {
var item = new TypeTwo(input[i]);
ret.push(item);
}
return ret;
}
function TypeOne(item) {
var self = this;
for (var prop in item) {
if (item.hasOwnProperty(prop)) {
self[prop] = item[prop];
}
}
self.name = getNameForId(item.id);
}
function TypeTwo(item) {
var self = this;
for (var prop in item) {
if (item.hasOwnProperty(prop)) {
self[prop] = item[prop];
}
}
self.name = getNameForId(item.id);
}
function extendDatasetOne() {
// add properties to prototype
}
function extendDatasetOne() {
// add properties to prototype
}
function getNameForId(id) {
var set = datacontext.lookups.items;
for (var i = 0; i < set.length; i++) {
if (id == set[i].id) {
return set[i].name;
}
}
return null;
}
}]);