Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
refactor(scope): separate controller from scope
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vojtajina committed Jan 23, 2012
1 parent f5343c9 commit 992c790
Show file tree
Hide file tree
Showing 34 changed files with 477 additions and 516 deletions.
16 changes: 8 additions & 8 deletions docs/content/api/angular.inputType.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ All `inputType` widgets support:
<doc:example>
<doc:source>
<script>
angular.inputType('json', function() {
this.$parseView = function() {
angular.inputType('json', function(element, scope) {
scope.$parseView = function() {
try {
this.$modelValue = angular.fromJson(this.$viewValue);
if (this.$error.JSON) {
Expand All @@ -52,19 +52,19 @@ All `inputType` widgets support:
}
}

this.$parseModel = function() {
scope.$parseModel = function() {
this.$viewValue = angular.toJson(this.$modelValue);
}
});

function Ctrl() {
this.data = {
function Ctrl($scope) {
$scope.data = {
framework:'angular',
codenames:'supper-powers'
}
this.required = false;
this.disabled = false;
this.readonly = false;
$scope.required = false;
$scope.disabled = false;
$scope.readonly = false;
}
</script>
<div ng:controller="Ctrl">
Expand Down
61 changes: 30 additions & 31 deletions docs/content/cookbook/advancedform.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -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">

Expand Down Expand Up @@ -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>
Expand Down
46 changes: 21 additions & 25 deletions docs/content/cookbook/deeplinking.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:'[email protected]'}]
};
}

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>
Expand Down
18 changes: 9 additions & 9 deletions docs/content/cookbook/form.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions docs/content/cookbook/helloworld.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
77 changes: 40 additions & 37 deletions docs/content/cookbook/mvc.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,69 @@ 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',
'text-align': 'center',
'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);
function row(row) { return same(b[row][0], b[row][1], b[row][2]);}
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>
Expand Down
16 changes: 8 additions & 8 deletions docs/content/guide/dev_guide.expressions.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ You can try evaluating different expressions here:
<doc:example>
<doc:source>
<script>
function Cntl2() {
this.exprs = [];
this.expr = '3*10|currency';
this.addExp = function(expr) {
function Cntl2($scope) {
$scope.exprs = [];
$scope.expr = '3*10|currency';
$scope.addExp = function(expr) {
this.exprs.push(expr);
};

this.removeExp = function(contact) {
$scope.removeExp = function(contact) {
for ( var i = 0, ii = this.exprs.length; i < ii; i++) {
if (contact === this.exprs[i]) {
this.exprs.splice(i, 1);
Expand Down Expand Up @@ -101,10 +101,10 @@ the global state (a common source of subtle bugs).
<doc:example>
<doc:source>
<script>
function Cntl1($window){
this.name = 'World';
function Cntl1($window, $scope){
$scope.name = 'World';

this.greet = function() {
$scope.greet = function() {
($window.mockWindow || $window).alert('Hello ' + this.name);
}
}
Expand Down
Loading

0 comments on commit 992c790

Please sign in to comment.