diff options
| author | Vojta Jina | 2011-11-29 21:51:59 -0800 | 
|---|---|---|
| committer | Vojta Jina | 2012-01-23 11:05:36 -0800 | 
| commit | 992c790f0786fa45c1cc3710f29bf49c7c322ba7 (patch) | |
| tree | 581d06ea9ba275a14d5891d83b2df03f9930bd45 /example/personalLog/personalLog.js | |
| parent | f5343c9fd3c7cd0fefdb4d71d2b579dbae998d6a (diff) | |
| download | angular.js-992c790f0786fa45c1cc3710f29bf49c7c322ba7.tar.bz2 | |
refactor(scope): separate controller from scope
Controller is standalone object, created using "new" operator, not messed up with scope anymore.
Instead, related scope is injected as $scope.
See design proposal: https://docs.google.com/document/pub?id=1SsgVj17ec6tnZEX3ugsvg0rVVR11wTso5Md-RdEmC0k
Closes #321
Closes #425
Breaks controller methods are not exported to scope automatically
Breaks Scope#$new() does not take controller as argument anymore
Diffstat (limited to 'example/personalLog/personalLog.js')
| -rw-r--r-- | example/personalLog/personalLog.js | 20 | 
1 files changed, 10 insertions, 10 deletions
| diff --git a/example/personalLog/personalLog.js b/example/personalLog/personalLog.js index 7be233cb..4d182227 100644 --- a/example/personalLog/personalLog.js +++ b/example/personalLog/personalLog.js @@ -26,26 +26,26 @@ var LOGS = 'logs';  /**   * The controller for the personal log app.   */ -function LogCtrl($cookieStore) { -  var self = this, -      logs = self.logs = $cookieStore.get(LOGS) || []; //main model +function LogCtrl($cookieStore, $scope) { + +  var logs = $scope.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; +  $scope.addLog = function(msg) { +    var newMsg = msg || $scope.newMsg;      if (!newMsg) return;      var log = {        at: new Date().getTime(),        msg: newMsg -    } +    };      logs.push(log);      $cookieStore.put(LOGS, logs); -    self.newMsg = ''; +    $scope.newMsg = '';    }; @@ -53,7 +53,7 @@ function LogCtrl($cookieStore) {     * Persistently removes a log from logs.     * @param {object} log The log to remove.     */ -  this.rmLog = function(log) { +  $scope.rmLog = function(log) {      for ( var i = 0; i < logs.length; i++) {        if (log === logs[i]) {          logs.splice(i, 1); @@ -68,14 +68,14 @@ function LogCtrl($cookieStore) {    /**     * Persistently removes all logs.     */ -  this.rmLogs = function() { +  $scope.rmLogs = function() {      logs.splice(0, logs.length);      $cookieStore.remove(LOGS);    };  }  //inject -LogCtrl.$inject = ['$cookieStore']; +LogCtrl.$inject = ['$cookieStore', '$scope'];  //export  example.personalLog.LogCtrl = LogCtrl; | 
