summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/ch05-03-simple-angular-service.js
diff options
context:
space:
mode:
authorAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
committerAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
commit13d05bc8458758ee39cb829098241e89616717ee (patch)
tree22a4d1ce65f15952f07a3df5af4b462b4697cb3a /framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/ch05-03-simple-angular-service.js
parent6139282e1e93c2322076de4b91b1c85d0bc4a8b3 (diff)
ONOS checkin based on commit tag e796610b1f721d02f9b0e213cf6f7790c10ecd60
Change-Id: Ife8810491034fe7becdba75dda20de4267bd15cd
Diffstat (limited to 'framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/ch05-03-simple-angular-service.js')
-rw-r--r--framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/ch05-03-simple-angular-service.js115
1 files changed, 115 insertions, 0 deletions
diff --git a/framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/ch05-03-simple-angular-service.js b/framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/ch05-03-simple-angular-service.js
new file mode 100644
index 00000000..25786bed
--- /dev/null
+++ b/framework/src/onos/web/gui/src/main/webapp/_sdh/ng-examples/ch05-03-simple-angular-service.js
@@ -0,0 +1,115 @@
+// ch05-03-simple-angular-service.js
+
+// this example shows three different ways of defining our own "service"...
+
+// use 'factory()' for functions/plain objects API
+// use 'service()' for JS class object API
+// use 'provider()' for configurable service API
+
+
+// this is a service definition
+function ItemServiceTwo() {
+ var items = [
+ {id: 0, label: 'Item 0'},
+ {id: 1, label: 'Item 1'}
+ ];
+ this.list = function () {
+ return items;
+ };
+ this.add = function (item) {
+ items.push(item);
+ };
+}
+
+// this is a provider definition
+function ItemServiceThree(optItems) {
+ var items = optItems || [];
+
+ this.list = function () {
+ return items;
+ };
+ this.add = function (item) {
+ items.push(item);
+ }
+}
+
+angular.module('notesApp', [])
+
+ // [provider] define item service as configurable provider
+ .provider('ItemServiceThree', function () {
+ var haveDefaultItems = true;
+
+ this.disableDefaultItems = function () {
+ haveDefaultItems = false;
+ };
+
+ // this function gets our dependencies..
+ this.$get = [function () {
+ var optItems = [];
+ if (haveDefaultItems) {
+ optItems = [
+ {id: 0, label: 'Item 0'},
+ {id: 1, label: 'Item 1'}
+ ];
+ }
+ return new ItemServiceThree(optItems);
+ }];
+ })
+
+ // [provider] define configuration for provider
+ .config(['ItemServiceThreeProvider', function (ItemServiceThreeProvider) {
+ // to see how the provider can change configuration
+ // change the value of shouldHaveDefaults to true and
+ // try running the example
+ var shouldHaveDefaults = false;
+
+ // get configuration from server.
+ // set shouldHaveDefaults somehow
+ // assume it magically changes for now
+ if (!shouldHaveDefaults) {
+ ItemServiceThreeProvider.disableDefaultItems();
+ }
+ }])
+
+ // [service] define item service as a JS class
+ .service('ItemServiceTwo', [ItemServiceTwo])
+
+ // [factory] define item service factory
+ .factory('ItemService', [function () {
+ var items = [
+ {id: 0, label: 'Item 0'},
+ {id: 1, label: 'Item 1'}
+ ];
+ return {
+ list: function () {
+ return items;
+ },
+ add: function (item) {
+ items.push(item);
+ }
+ };
+ }])
+
+ // ======================================================================
+ // define controllers...
+ .controller('MainCtrl', [function () {
+ var self = this;
+ self.tab = 'first';
+ self.open = function (tab) {
+ self.tab = tab;
+ };
+ }])
+
+ .controller('SubCtrl', ['ItemService', function (ItemService) {
+ var self = this;
+ self.list = function () {
+ return ItemService.list();
+ };
+ self.add = function () {
+ var n = self.list().length;
+ ItemService.add({
+ id: n,
+ label: 'Item ' + n
+ });
+ };
+ }]);