From 0d4def68ae0d95dd106d2731d60b6d6b635b5afc Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 2 Feb 2011 17:46:01 -0800 Subject: added more cookbook: work in progress --- docs/cookbook.mvc.ngdoc | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'docs/cookbook.mvc.ngdoc') diff --git a/docs/cookbook.mvc.ngdoc b/docs/cookbook.mvc.ngdoc index d63c1f25..2fa2c558 100644 --- a/docs/cookbook.mvc.ngdoc +++ b/docs/cookbook.mvc.ngdoc @@ -2,3 +2,102 @@ @ngdoc overview @name Cookbook: MVC @description + +MVC allows for a clean an testable separation between the behavior (controller) and the view (HTML template). A Controller is just a JavaScript class which is grafted onto the scope of the view. This makes it very easy for the controller and the view to share the model. + +The model is simply the controller's this. This makes it very easy to test the controller in isolation since one can simply instantiate the controller and test without a view, because there is no connection between the controller and the view. + + + + + +

Tic-Tac-Toe

+
+ Next Player: {{nextMove}} +
Player {{winner}} has won!
+ + + + +
{{cell}}
+ +
+
+ + +
+ + +Things to notice + +The controller is defined in JavaScript and has no reference to the rendering logic. +The controller is instantiated by and injected into the view. +The controller can be instantiated in isolation (without a view) and the code will still execute. This makes it very testable. +The HTML view is a projection of the model. In the above example, the model is stored in the board variable. +All of the controller's properties (such as board and nextMove) are available to the view. +Changing the model changes the view. +The view can call any controller function. +In this example, the setUrl() and readUrl() functions copy the game state to/from the URL's hash so the browser's back button will undo game steps. See deep-linking. This example calls $watch() to set up a listener that invokes readUrl() when needed. -- cgit v1.2.3