From 5b40e87ac687fc52ba86f39daf62975b169acf41 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 22 Oct 2010 22:13:14 -0700 Subject: personalLog demo - initial version with spec --- example/personalLog/personalLog.js | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 example/personalLog/personalLog.js (limited to 'example/personalLog/personalLog.js') diff --git a/example/personalLog/personalLog.js b/example/personalLog/personalLog.js new file mode 100644 index 00000000..dbd0956f --- /dev/null +++ b/example/personalLog/personalLog.js @@ -0,0 +1,60 @@ +//app namespace +var example = {}; +example.personalLog = {}; + + +//name space isolating closure +(function() { + +var LOGS = 'logs'; + +/** + * The controller for the personal log app. + */ +function LogCtrl($cookieStore) { + var self = this, + logs = self.logs = $cookieStore.get(LOGS) || []; + + + /** + * Adds newMsg to the logs array as a log, persists it and clears newMsg. + */ + 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 = ''; + } + + + /** + * Persistently removes a log from logs. + * @param {number} msgIdx Index of the log to remove. + */ + this.rmLog = function(msgIdx) { + logs.splice(msgIdx,1); + $cookieStore.put(LOGS, logs); + } + + + /** + * Persistently removes all logs. + */ + this.rmLogs = function() { + logs.splice(0); + $cookieStore.remove(LOGS); + } +} + +//inject +LogCtrl.$inject = ['$cookieStore']; + +//export +example.personalLog.LogCtrl = LogCtrl; +})(); \ No newline at end of file -- cgit v1.2.3