From c251432e54624f9e463348e664baad8e1291f177 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Sat, 27 Dec 2014 18:13:41 -0800 Subject: [PATCH] moved to angularAMD --- static/bower.json | 3 +- static/coffee/app.coffee | 31 +++++++++---------- static/coffee/controllers.coffee | 38 ------------------------ static/coffee/controllers/myctrl1.coffee | 19 ++++-------- static/coffee/controllers/myctrl2.coffee | 18 ++++------- static/coffee/directives.coffee | 14 --------- static/coffee/filters.coffee | 10 ------- static/coffee/main.coffee | 36 ++++++++-------------- static/coffee/routes.coffee | 17 ----------- static/coffee/services.coffee | 8 ----- static/index.html | 20 +++++++++++++ static/partials/partial1.html | 12 ++++---- static/partials/partial2.html | 15 +++++----- 13 files changed, 75 insertions(+), 166 deletions(-) delete mode 100644 static/coffee/controllers.coffee delete mode 100644 static/coffee/directives.coffee delete mode 100644 static/coffee/filters.coffee delete mode 100644 static/coffee/routes.coffee delete mode 100644 static/coffee/services.coffee diff --git a/static/bower.json b/static/bower.json index c408457..e9b0518 100644 --- a/static/bower.json +++ b/static/bower.json @@ -19,6 +19,7 @@ "requirejs": "~2.1.9", "requirejs-text": "~2.0.10", "foundation": "zurb/bower-foundation", - "html5-boilerplate": "~4.3.0" + "html5-boilerplate": "~4.3.0", + "angularAMD": "~0.2.1" } } diff --git a/static/coffee/app.coffee b/static/coffee/app.coffee index 5938c34..51c988d 100644 --- a/static/coffee/app.coffee +++ b/static/coffee/app.coffee @@ -1,17 +1,18 @@ define [ - "angular" - "filters" - "services" - "directives" - "controllers" - "angularRoute" -], (angular, filters, services, directives, controllers) -> + "angularAMD" + "angular-route" +], (angularAMD) -> + app = angular.module("webapp", ["ngRoute"]) + app.config ($routeProvider) -> + $routeProvider.when("/view1", angularAMD.route( + templateUrl: "partials/partial1.html" + controller: "MyCtrl1" + controllerUrl: "controllers/myctrl1" + )).when("/view2", angularAMD.route( + templateUrl: "partials/partial2.html" + controller: "MyCtrl2" + controllerUrl: "controllers/myctrl2" + )).otherwise redirectTo: "/view1" + return - # Declare app level module which depends on filters, and services - angular.module "myApp", [ - "ngRoute" - "myApp.filters" - "myApp.services" - "myApp.directives" - "myApp.controllers" - ] + angularAMD.bootstrap app diff --git a/static/coffee/controllers.coffee b/static/coffee/controllers.coffee deleted file mode 100644 index a08bf2b..0000000 --- a/static/coffee/controllers.coffee +++ /dev/null @@ -1,38 +0,0 @@ -define ["angular", "services"], (angular) -> - - # Controllers - - # Sample controller where service is being used - - # More involved example where controller is required from an external file - controllers = angular.module("myApp.controllers", ["myApp.services"]) - - controllers.controller("MyCtrl1", [ - "$scope" - "$injector" - ($scope, $injector) -> - require ["controllers/myctrl1"], (myctrl1) -> - - # injector method takes an array of modules as the first argument - # if you want your controller to be able to use components from - # any of your other modules, make sure you include it together with 'ng' - # Furthermore we need to pass on the $scope as it's unique to this controller - $injector.invoke(myctrl1, this, {$scope: $scope}) - - return - ]) - - controllers.controller( "MyCtrl2", [ - "$scope" - "$injector" - ($scope, $injector) -> - require ["controllers/myctrl2"], (myctrl2) -> - - # injector method takes an array of modules as the first argument - # if you want your controller to be able to use components from - # any of your other modules, make sure you include it together with 'ng' - # Furthermore we need to pass on the $scope as it's unique to this controller - $injector.invoke(myctrl2, this, {$scope: $scope}) - - return - ]) diff --git a/static/coffee/controllers/myctrl1.coffee b/static/coffee/controllers/myctrl1.coffee index 9e7508e..cba2917 100644 --- a/static/coffee/controllers/myctrl1.coffee +++ b/static/coffee/controllers/myctrl1.coffee @@ -1,15 +1,6 @@ -define [], -> - [ - "$scope" - "$http" - ($scope, $http) -> +define ["app"], (app) -> + app.controller "MyCtrl1", ($scope) -> + $scope.message = "Message from MyCtrl1" + return - # You can access the scope of the controller from here - $scope.welcomeMessage = "hey this is myctrl1.coffee!" - $scope.okay = "okay?" - - # because this has happened asynchroneusly we've missed - # Angular's initial call to $apply after the controller has been loaded - # hence we need to explicityly call it at the end of our Controller constructor - $scope.$apply() - ] + return diff --git a/static/coffee/controllers/myctrl2.coffee b/static/coffee/controllers/myctrl2.coffee index eefba9c..c13be1a 100644 --- a/static/coffee/controllers/myctrl2.coffee +++ b/static/coffee/controllers/myctrl2.coffee @@ -1,14 +1,6 @@ -define [], -> - [ - "$scope" - "$http" - ($scope, $http) -> +define ["app"], (app) -> + app.controller "MyCtrl2", ($scope) -> + $scope.message = "Message from MyCtrl2" + return - # You can access the scope of the controller from here - $scope.welcomeMessage = "hey this is myctrl2.coffee!" - - # because this has happened asynchroneusly we've missed - # Angular's initial call to $apply after the controller has been loaded - # hence we need to explicityly call it at the end of our Controller constructor - $scope.$apply() - ] + return diff --git a/static/coffee/directives.coffee b/static/coffee/directives.coffee deleted file mode 100644 index de6c134..0000000 --- a/static/coffee/directives.coffee +++ /dev/null @@ -1,14 +0,0 @@ -define [ - "angular" - "services" -], (angular, services) -> - - # Directives - angular.module("myApp.directives", ["myApp.services"]).directive "appVersion", [ - "version" - (version) -> - return (scope, elm, attrs) -> - elm.text version - return - ] - return diff --git a/static/coffee/filters.coffee b/static/coffee/filters.coffee deleted file mode 100644 index 2eb5357..0000000 --- a/static/coffee/filters.coffee +++ /dev/null @@ -1,10 +0,0 @@ -define ["angular", "services"], (angular, services) -> - - # Filters - angular.module("myApp.filters", ["myApp.services"]).filter "interpolate", [ - "version" - (version) -> - return (text) -> - String(text).replace /\%VERSION\%/g, version - ] - return diff --git a/static/coffee/main.coffee b/static/coffee/main.coffee index 1ce0d53..ebda0c0 100644 --- a/static/coffee/main.coffee +++ b/static/coffee/main.coffee @@ -1,28 +1,18 @@ require.config - paths: - angular: "/bower_components/angular/angular" - angularRoute: "/bower_components/angular-route/angular-route" - angularMocks: "/bower_components/angular-mocks/angular-mocks" - text: "/bower_components/requirejs-text/text" + baseUrl: "js" - shim: - angular: - exports: "angular" - - angularRoute: ["angular"] - angularMocks: - deps: ["angular"] - exports: "angular.mock" - - priority: ["angular"] + # alias libraries paths. Must set 'angular' + paths: + "angular" : "/bower_components/angular/angular.min" + "angular-route": "/bower_components/angular-route/angular-route.min" + "angularAMD" : "/bower_components/angularAMD/angularAMD.min" -#http://code.angularjs.org/1.2.1/docs/guide/bootstrap#overview_deferred-bootstrap -window.name = "NG_DEFER_BOOTSTRAP!" -require ["angular", "app", "routes"], (angular, app, routes) -> - $html = angular.element(document.getElementsByTagName("html")[0]) - angular.element().ready -> - angular.resumeBootstrap [app["name"]] - return + # Add angular modules that does not support AMD out of the box, put it in a shim + shim: + angularAMD: ["angular"] + "angular-route": ["angular"] - return + + # kick start application + deps: ["app"] diff --git a/static/coffee/routes.coffee b/static/coffee/routes.coffee deleted file mode 100644 index bbc89f6..0000000 --- a/static/coffee/routes.coffee +++ /dev/null @@ -1,17 +0,0 @@ -define [ - "angular" - "app" -], (angular, app) -> - app.config [ - "$routeProvider" - ($routeProvider) -> - $routeProvider.when "/view1", - templateUrl: "/partials/partial1.html" - controller: "MyCtrl1" - - $routeProvider.when "/view2", - templateUrl: "/partials/partial2.html" - controller: "MyCtrl2" - - $routeProvider.otherwise redirectTo: "/view1" - ] diff --git a/static/coffee/services.coffee b/static/coffee/services.coffee deleted file mode 100644 index 31d1798..0000000 --- a/static/coffee/services.coffee +++ /dev/null @@ -1,8 +0,0 @@ -define ["angular"], (angular) -> - - # Services - - # Demonstrate how to register services - # In this case it is a simple value service. - angular.module("myApp.services", []).value "version", "0.1" - return diff --git a/static/index.html b/static/index.html index 11dd6ca..66a6dcd 100644 --- a/static/index.html +++ b/static/index.html @@ -1,3 +1,5 @@ + + + + + + + + + + + + + +

Sample for angularAMD

+
+ + + diff --git a/static/partials/partial1.html b/static/partials/partial1.html index a4e1ea9..04b7d12 100644 --- a/static/partials/partial1.html +++ b/static/partials/partial1.html @@ -1,5 +1,7 @@ -

This is the partial for view 1.

-

- Getting version from the scope: {{ scopedAppVersion }} -

-

{{ okay }}

+

View 1

+ +
View Message: {{message}}
+ +
+ +View 2 diff --git a/static/partials/partial2.html b/static/partials/partial2.html index 68c6012..c093306 100644 --- a/static/partials/partial2.html +++ b/static/partials/partial2.html @@ -1,8 +1,7 @@ -

This is the partial for view 2.

-

- {{ welcomeMessage }} -

-

- Showing of 'interpolate' filter: - {{ 'Current version is v%VERSION%.' | interpolate }} -

+

View2

+ +
View Message: {{message}}
+ +
+ +view 1