aboutsummaryrefslogtreecommitdiffstats
path: root/example/personalLog/personalLog.js
blob: c027303695ebdd02c4f15a833164f5e1ed6d92c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * @fileOverview Very simple personal log demo application to demostrate angular functionality,
 *               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 */
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) || []; //main model


  /**
   * Adds newMsg to the logs array as a log, persists it and clears newMsg.
   * @param {string} msg Message to add (message is passed as parameter to make testing easier).
   */
  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;
})();