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/advancedform.ngdoc | |
| 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/advancedform.ngdoc')
| -rw-r--r-- | docs/content/cookbook/advancedform.ngdoc | 61 | 
1 files changed, 30 insertions, 31 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>  | 
