Skip to content

Commit 57f0611

Browse files
committed
Add slave controller and move some of the master directive features into the slave controller.
1 parent 052e72b commit 57f0611

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

app/controllers/master-controller.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@
5353
};
5454

5555

56-
// I reposition the given slave.
57-
$scope.repositionSlave = function( slave, x, y ) {
58-
59-
slave.x = x;
60-
slave.y = y;
61-
62-
};
63-
64-
6556
// -- Set Scope Variables. ---------------------- //
6657

6758

app/controllers/slave-controller.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(function( ng, app ) {
2+
3+
"use strict";
4+
5+
app.controller(
6+
"SlaveController",
7+
function( $scope ) {
8+
9+
10+
// -- Define Scope Methods. --------------------- //
11+
12+
13+
// I remove the current slave from the collection.
14+
$scope.remove = function() {
15+
16+
// Pass this responsibility up the scope chain to the master controller (and its
17+
// collection of slave instances).
18+
$scope.removeSlave( $scope.slave );
19+
20+
};
21+
22+
23+
// I reposition the current slave.
24+
$scope.reposition = function( x, y ) {
25+
26+
$scope.slave.x = x;
27+
$scope.slave.y = y;
28+
29+
};
30+
31+
32+
}
33+
);
34+
35+
})( angular, demo );

app/directives/master.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@
110110
element.on( "mousemove.bnMaster", handleMouseMove );
111111
element.on( "mouseup.bnMaster", handleMouseUp );
112112

113-
// The user clicked on the master directly.
113+
// The user clicked on the master canvas directly. We'll use this as an invite
114+
// to create a new slave handle.
114115
} else {
115116

116117
$scope.$apply(
@@ -126,8 +127,7 @@
126127
}
127128

128129

129-
// I listen for mouse movements to broadcast new position deltas to all of
130-
// the slaves.
130+
// I listen for mouse movements to broadcast new position deltas to all of the slaves.
131131
function handleMouseMove( event ) {
132132

133133
controller.moveTo(

app/directives/slave.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
// the slave directly, as this WILL happen inside of a $digest.
3333
function reposition( deltaX, deltaY ) {
3434

35-
$scope.repositionSlave(
36-
$scope.slave,
35+
$scope.reposition(
3736
( $scope.slave.x + deltaX ),
3837
( $scope.slave.y + deltaY )
3938
);
@@ -96,7 +95,7 @@
9695
$scope.$apply(
9796
function() {
9897

99-
$scope.removeSlave( $scope.slave );
98+
$scope.remove();
10099

101100
}
102101
);
@@ -125,6 +124,11 @@
125124
"$destroy",
126125
function( event ) {
127126

127+
// Clean up the master-slave binding in case this element is removed outside
128+
// of our internal event handling.
129+
masterController.unbind( slaveController );
130+
131+
// Clear any existing mouse bindings.
128132
element.off( "mousedown.bnSlave" );
129133
$document.off( "mousemove.bnSlave" );
130134
$document.off( "mouseup.bnSlave" );

index.htm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1>
1414
</h1>
1515

1616

17-
<!-- BEGIN: Master. -->
17+
<!-- BEGIN: Master Canvas. -->
1818
<div
1919
ng-controller="MasterController"
2020
bn-master
@@ -25,6 +25,7 @@ <h1>
2525
<ol class="handles">
2626
<li
2727
ng-repeat="slave in slaves"
28+
ng-controller="SlaveController"
2829
bn-slave
2930
class="slave"
3031
ng-style="{ left: ( slave.x + 'px' ), top: ( slave.y + 'px' ) }">
@@ -55,7 +56,7 @@ <h1>
5556

5657

5758
</div>
58-
<!-- END: Master. -->
59+
<!-- END: Master Canvas. -->
5960

6061

6162
<!-- Load jQuery and AngularJS from the CDN. -->
@@ -71,6 +72,7 @@ <h1>
7172
<!-- Load the app module and its classes. -->
7273
<script type="text/javascript" src="app/main.js"></script>
7374
<script type="text/javascript" src="app/controllers/master-controller.js"></script>
75+
<script type="text/javascript" src="app/controllers/slave-controller.js"></script>
7476
<script type="text/javascript" src="app/directives/master.js"></script>
7577
<script type="text/javascript" src="app/directives/slave.js"></script>
7678

0 commit comments

Comments
 (0)