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 /docs/content/cookbook | |
| 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 'docs/content/cookbook')
| -rw-r--r-- | docs/content/cookbook/advancedform.ngdoc | 61 | ||||
| -rw-r--r-- | docs/content/cookbook/deeplinking.ngdoc | 46 | ||||
| -rw-r--r-- | docs/content/cookbook/form.ngdoc | 18 | ||||
| -rw-r--r-- | docs/content/cookbook/helloworld.ngdoc | 4 | ||||
| -rw-r--r-- | docs/content/cookbook/mvc.ngdoc | 77 |
5 files changed, 102 insertions, 104 deletions
diff --git a/docs/content/cookbook/advancedform.ngdoc b/docs/content/cookbook/advancedform.ngdoc index e973e30f..58a8dfd5 100644 --- a/docs/content/cookbook/advancedform.ngdoc +++ b/docs/content/cookbook/advancedform.ngdoc @@ -8,10 +8,8 @@ detection, and preventing invalid form submission. <doc:example> <doc:source> <script> - function UserForm() { - this.state = /^\w\w$/; - this.zip = /^\d\d\d\d\d$/; - this.master = { + function UserForm($scope) { + var master = { name: 'John Smith', address:{ line1: '123 Main St.', @@ -23,40 +21,42 @@ detection, and preventing invalid form submission. {type:'phone', value:'1(234) 555-1212'} ] }; - this.cancel(); - } - UserForm.prototype = { - cancel: function() { - this.form = angular.copy(this.master); - }, + $scope.state = /^\w\w$/; + $scope.zip = /^\d\d\d\d\d$/; - save: function() { - this.master = this.form; - this.cancel(); - }, + $scope.cancel = function() { + $scope.form = angular.copy(master); + }; - addContact: function() { - this.form.contacts.push({type:'', value:''}); - }, + $scope.save = function() { + master = $scope.form; + $scope.cancel(); + }; + + $scope.addContact = function() { + $scope.form.contacts.push({type:'', value:''}); + }; - removeContact: function(contact) { - for ( var i = 0, ii = this.form.contacts.length; i < ii; i++) { - if (contact === this.form.contacts[i]) { - this.form.contacts.splice(i, 1); + $scope.removeContact = function(contact) { + var contacts = $scope.form.contacts; + for (var i = 0, ii = contacts.length; i < ii; i++) { + if (contact === contacts[i]) { + contacts.splice(i, 1); } } - }, + }; - isCancelDisabled: function() { - return angular.equals(this.master, this.form); - }, + $scope.isCancelDisabled = function() { + return angular.equals(master, $scope.form); + }; - isSaveDisabled: function() { - return this.myForm.$invalid || angular.equals(this.master, this.form); - } + $scope.isSaveDisabled = function() { + return $scope.myForm.$invalid || angular.equals(master, $scope.form); + }; - }; + $scope.cancel(); + } </script> <div ng:controller="UserForm"> @@ -91,8 +91,7 @@ detection, and preventing invalid form submission. <hr/> Debug View: - <pre>form={{form}} - master={{master}}</pre> + <pre>form={{form}}</pre> </div> </doc:source> <doc:scenario> diff --git a/docs/content/cookbook/deeplinking.ngdoc b/docs/content/cookbook/deeplinking.ngdoc index 2ef3da4a..a4dc3a9b 100644 --- a/docs/content/cookbook/deeplinking.ngdoc +++ b/docs/content/cookbook/deeplinking.ngdoc @@ -39,42 +39,38 @@ The two partials are defined in the following URLs: <doc:example> <doc:source jsfiddle="false"> <script> - AppCntl.$inject = ['$route'] - function AppCntl($route) { + AppCntl.$inject = ['$scope', '$route'] + function AppCntl($scope, $route) { // define routes $route.when("/welcome", {template:'./examples/welcome.html', controller:WelcomeCntl}); $route.when("/settings", {template:'./examples/settings.html', controller:SettingsCntl}); - $route.parent(this); + $route.parent($scope); // initialize the model to something useful - this.person = { + $scope.person = { name:'anonymous', contacts:[{type:'email', url:'anonymous@example.com'}] }; } - function WelcomeCntl($route){} - WelcomeCntl.prototype = { - greet: function() { - alert("Hello " + this.person.name); - } - }; - - SettingsCntl.$inject = ['$location']; - function SettingsCntl($location){ - this.$location = $location; - this.cancel(); + function WelcomeCntl($scope) { + $scope.greet = function() { + alert("Hello " + $scope.person.name); + }; + } + + function SettingsCntl($scope, $location) { + $scope.cancel = function() { + $scope.form = angular.copy($scope.person); + }; + + $scope.save = function() { + angular.copy($scope.form, $scope.person); + $location.path('/welcome'); + }; + + $scope.cancel(); } - SettingsCntl.prototype = { - cancel: function() { - this.form = angular.copy(this.person); - }, - - save: function() { - angular.copy(this.form, this.person); - this.$location.path('/welcome'); - } - }; </script> <div ng:controller="AppCntl"> <h1>Your App Chrome</h1> diff --git a/docs/content/cookbook/form.ngdoc b/docs/content/cookbook/form.ngdoc index 80c23e94..9371da7a 100644 --- a/docs/content/cookbook/form.ngdoc +++ b/docs/content/cookbook/form.ngdoc @@ -10,23 +10,23 @@ allow a user to enter data. <doc:example> <doc:source> <script> - function FormController() { - this.user = { + function FormController($scope) { + $scope.user = { name: 'John Smith', address:{line1: '123 Main St.', city:'Anytown', state:'AA', zip:'12345'}, contacts:[{type:'phone', value:'1(234) 555-1212'}] }; - this.state = /^\w\w$/; - this.zip = /^\d\d\d\d\d$/; + $scope.state = /^\w\w$/; + $scope.zip = /^\d\d\d\d\d$/; - this.addContact = function() { - this.user.contacts.push({type:'', value:''}); + $scope.addContact = function() { + $scope.user.contacts.push({type:'', value:''}); }; - this.removeContact = function(contact) { - for ( var i = 0, ii = this.user.contacts.length; i < ii; i++) { + $scope.removeContact = function(contact) { + for (var i = 0, ii = this.user.contacts.length; i < ii; i++) { if (contact === this.user.contacts[i]) { - this.user.contacts.splice(i, 1); + $scope.user.contacts.splice(i, 1); } } }; diff --git a/docs/content/cookbook/helloworld.ngdoc b/docs/content/cookbook/helloworld.ngdoc index e3d76d83..b6b5bcf7 100644 --- a/docs/content/cookbook/helloworld.ngdoc +++ b/docs/content/cookbook/helloworld.ngdoc @@ -5,8 +5,8 @@ <doc:example> <doc:source> <script> - function HelloCntl() { - this.name = 'World'; + function HelloCntl($scope) { + $scope.name = 'World'; } </script> <div ng:controller="HelloCntl"> diff --git a/docs/content/cookbook/mvc.ngdoc b/docs/content/cookbook/mvc.ngdoc index f566a541..71e771bd 100644 --- a/docs/content/cookbook/mvc.ngdoc +++ b/docs/content/cookbook/mvc.ngdoc @@ -14,9 +14,8 @@ no connection between the controller and the view. <doc:example> <doc:source> <script> - function TicTacToeCntl($location){ - this.$location = $location; - this.cellStyle= { + function TicTacToeCntl($scope, $location) { + $scope.cellStyle= { 'height': '20px', 'width': '20px', 'border': '1px solid black', @@ -24,30 +23,40 @@ no connection between the controller and the view. 'vertical-align': 'middle', 'cursor': 'pointer' }; - this.reset(); - this.$watch('$location.search().board', this.readUrl); - } - TicTacToeCntl.prototype = { - dropPiece: function(row, col) { - if (!this.winner && !this.board[row][col]) { - this.board[row][col] = this.nextMove; - this.nextMove = this.nextMove == 'X' ? 'O' : 'X'; - this.setUrl(); - } - }, - reset: function() { - this.board = [ + + $scope.reset = function() { + $scope.board = [ ['', '', ''], ['', '', ''], ['', '', ''] ]; - this.nextMove = 'X'; - this.winner = ''; - this.setUrl(); - }, - grade: function() { - var b = this.board; - this.winner = + $scope.nextMove = 'X'; + $scope.winner = ''; + setUrl(); + }; + + $scope.dropPiece = function(row, col) { + if (!$scope.winner && !$scope.board[row][col]) { + $scope.board[row][col] = $scope.nextMove; + $scope.nextMove = $scope.nextMove == 'X' ? 'O' : 'X'; + setUrl(); + } + }; + + $scope.reset(); + $scope.$watch(function() { return $location.search().board;}, readUrl); + + function setUrl() { + var rows = []; + angular.forEach($scope.board, function(row) { + rows.push(row.join(',')); + }); + $location.search({board: rows.join(';') + '/' + $scope.nextMove}); + } + + function grade() { + var b = $scope.board; + $scope.winner = row(0) || row(1) || row(2) || col(0) || col(1) || col(2) || diagonal(-1) || diagonal(1); @@ -55,25 +64,19 @@ no connection between the controller and the view. function col(col) { return same(b[0][col], b[1][col], b[2][col]);} function diagonal(i) { return same(b[0][1-i], b[1][1], b[2][1+i]);} function same(a, b, c) { return (a==b && b==c) ? a : '';}; - }, - setUrl: function() { - var rows = []; - angular.forEach(this.board, function(row){ - rows.push(row.join(',')); - }); - this.$location.search({board: rows.join(';') + '/' + this.nextMove}); - }, - readUrl: function(scope, value) { + } + + function readUrl(scope, value) { if (value) { value = value.split('/'); - this.nextMove = value[1]; + $scope.nextMove = value[1]; angular.forEach(value[0].split(';'), function(row, col){ - this.board[col] = row.split(','); - }, this); - this.grade(); + $scope.board[col] = row.split(','); + }); + grade(); } } - }; + } </script> <h3>Tic-Tac-Toe</h3> |
