Skip to content

Commit

Permalink
Now able to add to repos and listen from models
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremythuff committed Nov 15, 2016
1 parent 2e07ad7 commit 48c2bbf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
34 changes: 23 additions & 11 deletions app/model/abstractModel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
core.factory("AbstractModel", function ($q, $sanitize, $timeout, WsApi, ValidationStore) {
core.factory("AbstractModel", function ($rootScope, $q, $sanitize, $timeout, WsApi, ValidationStore) {

return function AbstractModel() {

Expand All @@ -17,12 +17,15 @@ core.factory("AbstractModel", function ($q, $sanitize, $timeout, WsApi, Validati
var validations;

var validationResults = {};

$rootScope.$on("$locationChangeSuccess", function() {
listenCallbacks.length = 0;
});

var fetch = function() {
if(mapping.instantiate !== undefined) {
WsApi.fetch(mapping.instantiate).then(function(res) {
processResponse(res);
listen();
processResponse(res);
});
}
}
Expand All @@ -45,6 +48,9 @@ core.factory("AbstractModel", function ($q, $sanitize, $timeout, WsApi, Validati
fetch();
}
}

listen();

};

$timeout(function() {
Expand Down Expand Up @@ -151,15 +157,19 @@ core.factory("AbstractModel", function ($q, $sanitize, $timeout, WsApi, Validati
};

var listen = function() {
angular.extend(mapping.listen, {method: abstractModel.id});
var notifyPromise = WsApi.listen(mapping.listen);
notifyPromise.then(null, null, function(res) {
processResponse(res);
angular.forEach(listenCallbacks, function(cb) {
cb(res);

if(abstractModel.id && mapping.listen) {
angular.extend(mapping.listen, {method: "/"+abstractModel.id});
var notifyPromise = WsApi.listen(mapping.listen);
notifyPromise.then(null, null, function(res) {
processResponse(res);
angular.forEach(listenCallbacks, function(cb) {
cb(res);
});
});
});
return notifyPromise;
return notifyPromise;
}

};

var processResponse = function(res) {
Expand All @@ -170,9 +180,11 @@ core.factory("AbstractModel", function ($q, $sanitize, $timeout, WsApi, Validati

if(meta.type != 'ERROR') {
var payload = resObj.payload;

angular.forEach(payload, function(datum) {
angular.extend(abstractModel, datum);
});

setData(abstractModel);
}
else {
Expand Down
35 changes: 25 additions & 10 deletions app/repo/abstractRepo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
core.service("AbstractRepo", function ($rootScope, $q, WsApi, ValidationStore) {

return function AbstractRepo(model, mapping) {

Expand All @@ -18,14 +18,19 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {

var validations = ValidationStore.getValidations(entityName);

$rootScope.$on("$locationChangeSuccess", function() {
listenCallbacks.length = 0;
});

var build = function(data) {

initialized = false;
return $q(function(resolve) {
list.length = 0;
angular.forEach(data, function(modelJson) {
list.push(new model(modelJson));
});
initialized = true
initialized = true;
resolve();
});
};
Expand All @@ -36,7 +41,7 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
var keys = Object.keys(payload);
angular.forEach(keys, function(key) {
angular.extend(repoObj, payload[key]);
})
});
return repoObj;
};

Expand Down Expand Up @@ -64,6 +69,16 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {

abstractRepo.ValidationResults = {};

abstractRepo.add = function(modelJson) {
initialized = true;
build([modelJson]);
};

abstractRepo.addAll = function(modelJsons) {
initialized = true;
build(modelJsons);
};

abstractRepo.getEntityName = function() {
return entityName;
};
Expand All @@ -74,7 +89,7 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {

abstractRepo.count = function() {
if(!initialized) {
console.error('Repo not initialized!');
console.warn('Repo ('+entityName+') not initialized!');
}
return list.length;
};
Expand All @@ -83,6 +98,10 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
if(mapping.lazy) {
fetch();
}
return abstractRepo.getContents();
};

abstractRepo.getContents = function() {
return list;
};

Expand Down Expand Up @@ -110,14 +129,14 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
return list[key];
}
}
}
};

if(initialized) {
match = find(id);
}
else {
// TODO: think of a way to find after ready and have binding in list
console.error("Repo not initialized!");
console.warn('Repo ('+entityName+') not initialized!');
}

return match;
Expand All @@ -129,7 +148,6 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
promise.then(function(res) {
if(angular.fromJson(res.body).meta.type == "INVALID") {
angular.extend(abstractRepo, angular.fromJson(res.body).payload);
console.log(abstractRepo);
}
});
return promise;
Expand All @@ -141,7 +159,6 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
promise.then(function(res) {
if(angular.fromJson(res.body).meta.type == "INVALID") {
angular.extend(abstractRepo, angular.fromJson(res.body).payload);
console.log(abstractRepo);
}
});
return promise;
Expand All @@ -154,7 +171,6 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
promise.then(function(res) {
if(angular.fromJson(res.body).meta.type == "INVALID") {
angular.extend(abstractRepo, angular.fromJson(res.body).payload);
console.log(abstractRepo);
}
});
return promise;
Expand All @@ -167,7 +183,6 @@ core.service("AbstractRepo", function ($q, WsApi, ValidationStore) {
promise.then(function(res) {
if(angular.fromJson(res.body).meta.type == "INVALID") {
angular.extend(abstractRepo, angular.fromJson(res.body).payload);
console.log(abstractRepo);
}
});
return promise;
Expand Down

0 comments on commit 48c2bbf

Please sign in to comment.