2010-10-23 23:25:53 +00:00
|
|
|
/**
|
2010-10-26 22:35:58 +00:00
|
|
|
* @fileOverview Very simple personal log demo application to demonstrate angular functionality,
|
2010-10-23 23:25:53 +00:00
|
|
|
* especially:
|
|
|
|
|
* - the MVC model
|
|
|
|
|
* - testability of controllers
|
|
|
|
|
* - dependency injection for controllers via $inject and constructor function
|
|
|
|
|
* - $cookieStore for persistent cookie-backed storage
|
|
|
|
|
* - simple templating constructs such as ng:repeat and {{}}
|
|
|
|
|
* - date filter
|
|
|
|
|
* - and binding onSubmit and onClick events to angular expressions
|
|
|
|
|
* @author Igor Minar
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @namespace the 'example' namespace */
|
|
|
|
|
var example = example || {};
|
|
|
|
|
/** @namespace namespace of the personal log app */
|
2010-10-23 05:13:14 +00:00
|
|
|
example.personalLog = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//name space isolating closure
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
|
var LOGS = 'logs';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The controller for the personal log app.
|
|
|
|
|
*/
|
|
|
|
|
function LogCtrl($cookieStore) {
|
|
|
|
|
var self = this,
|
2010-10-23 23:25:53 +00:00
|
|
|
logs = self.logs = $cookieStore.get(LOGS) || []; //main model
|
2010-10-23 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds newMsg to the logs array as a log, persists it and clears newMsg.
|
2010-10-23 23:25:53 +00:00
|
|
|
* @param {string} msg Message to add (message is passed as parameter to make testing easier).
|
2010-10-23 05:13:14 +00:00
|
|
|
*/
|
|
|
|
|
this.addLog = function(msg) {
|
|
|
|
|
var newMsg = msg || self.newMsg;
|
|
|
|
|
if (!newMsg) return;
|
|
|
|
|
var log = {
|
|
|
|
|
at: new Date().getTime(),
|
|
|
|
|
msg: newMsg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logs.push(log);
|
|
|
|
|
$cookieStore.put(LOGS, logs);
|
|
|
|
|
self.newMsg = '';
|
2010-10-26 22:35:58 +00:00
|
|
|
};
|
2010-10-23 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Persistently removes a log from logs.
|
2010-10-24 04:40:27 +00:00
|
|
|
* @param {object} log The log to remove.
|
2010-10-23 05:13:14 +00:00
|
|
|
*/
|
2010-10-24 04:40:27 +00:00
|
|
|
this.rmLog = function(log) {
|
|
|
|
|
angular.Array.remove(logs, log);
|
2010-10-23 05:13:14 +00:00
|
|
|
$cookieStore.put(LOGS, logs);
|
2010-10-26 22:35:58 +00:00
|
|
|
};
|
2010-10-23 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Persistently removes all logs.
|
|
|
|
|
*/
|
|
|
|
|
this.rmLogs = function() {
|
2010-10-26 22:35:58 +00:00
|
|
|
logs.splice(0, logs.length);
|
2010-10-23 05:13:14 +00:00
|
|
|
$cookieStore.remove(LOGS);
|
2010-10-26 22:35:58 +00:00
|
|
|
};
|
2010-10-23 05:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//inject
|
|
|
|
|
LogCtrl.$inject = ['$cookieStore'];
|
|
|
|
|
|
|
|
|
|
//export
|
|
|
|
|
example.personalLog.LogCtrl = LogCtrl;
|
|
|
|
|
})();
|