<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8" />
<title>Simple Breeze Todo</title>
<link rel="stylesheet" href="http://sampleservice.breezejs.com/content/styles.css" />
</head>
<body id="view" style="margin-left:8px;" ng-app="app" ng-controller="main as vm" class="ng-cloak view">
<h1>Breeze Todos with AngularJS</h1>
<ul class="items">
<li data-ng-repeat="todo in vm.todos">
<input type="checkbox" data-ng-model="todo.isDone" />
<label data-ng-bind="todo.description" autofocus="" data-ng-class="{ done: todo.isDone, archived: todo.isArchived }"></label>
</li>
</ul>
<label class="error" data-ng-show="vm.errorMessage">{{vm.errorMessage}}</label>
<p>
<button data-ng-click="vm.save()">Save</button>
<button data-ng-click="vm.reset()">Reset Db</button>
</p>
<p>
<a href="http://www.breezejs.com" target="_blank">Breeze Home</a>
</p>
<h3>Log:</h3>
<div data-ng-repeat="log in vm.logList">{{log}}</div>
<!-- 3rd party libs -->
<script data-require="angular.js@*" data-semver="1.3.0" src="//code.angularjs.org/1.3.0/angular.js"></script>
<script src="https://cdn.rawgit.com/Breeze/breeze.js/master/build/breeze.debug.js"></script>
<script src="https://cdn.rawgit.com/Breeze/breeze.js.labs/master/breeze.angular.js"></script>
<script src="script.js"></script>
<script src="http://sampleservice.breezejs.com/scripts/plunkerHelpers.js"></script>
<!-- app libs -->
<script>
document.write('<base href="' + document.location + '" />');
</script>
<script src="app.js"></script>
<script src="datacontext.js"></script>
<script src="main.js"></script>
<script src="logger.js"></script>
</body>
</html>
(function() {
angular.module('app').factory('datacontext',
['$http', 'logger', 'breeze', datacontext]);
function datacontext($http, logger, breeze) {
var log = logger.log;
var logError = logger.logError;
log("Creating datacontext");
// convert server PascalCase property names to camelCase
breeze.NamingConvention.camelCase.setAsDefault();
// create a new manager talking to sample service
var host="http://sampleservice.breezejs.com";
var serviceName = host+"/api/todos";
var manager = new breeze.EntityManager(serviceName);
plunkerHelpers.isCorsCapable();
var service = {
getAllTodos: getAllTodos,
save: save,
reset: reset
};
return service;
/*** implementation ***/
function getAllTodos() {
log("Getting Todos");
return breeze.EntityQuery.from("Todos")
.using(manager).execute()
.then(success)
.catch(function(error){ opFailed('Get Todos', error)} );
function success(data) {
log("Retrieved " + data.results.length);
return data.results;
}
}
function opFailed(operation, error){
logError(operation + " failed: \n"+error);
throw error; // re-throw so next in promise chain sees it
}
function save(){
var changeCount = manager.getChanges().length;
var msg = (save)
? "Saving "+ changeCount + " change(s) ..."
: "No changes to save";
log(msg);
return manager
.saveChanges()
.then( function (data) {
log("Saved " + changeCount);}
)
.catch(function(error) { opFailed('Save', error)} );
}
function reset() {
log("Resetting the data to initial state");
manager.clear();
return $http.post(serviceName + '/reset')
.then( function (data) { log("Database reset");} )
.catch(function(error) { opFailed('Database reset', error)} );
}
}
})();
angular.module('app', ['breeze.angular']);
(function() {
angular.module('app').factory('logger', ['$log', logger]);
function logger ($log){
var msgCounter = 1;
var service = {
log: log,
logError: logError,
logList: []
};
return service;
function log (text) {
text = mkMsg(text);
service.logList.push(text);
$log.log(text);
}
function logError (text) {
text = mkMsg(text);
service.logList.push(text);
$log.error(text);
}
function mkMsg(text){
return msgCounter++ + ": " + text;
}
}
})();
(function() {
angular.module('app').controller('main',
[ 'logger', 'datacontext', controller]);
function controller(logger, datacontext) {
var vm = this;
vm.errorMessage = '';
vm.logList = logger.logList;
vm.reset = reset;
vm.save = save;
vm.todos = [];
getTodos();
function getTodos() {
datacontext.getAllTodos()
.then(success)
.catch(failed);
function success(data) {
vm.todos = data;
}
function failed(error) {
vm.errorMessage = error.message;
}
}
function save(){
datacontext.save().catch(function(error){
vm.errorMessage = error.message;
})
}
function reset() {
datacontext.reset().then(getTodos);
}
}
})();