Simon Hunt | c022775 | 2014-12-11 16:46:57 -0800 | [diff] [blame] | 1 | // ch05-03-simple-angular-service.js |
| 2 | |
| 3 | // this example shows three different ways of defining our own "service"... |
| 4 | |
| 5 | // use 'factory()' for functions/plain objects API |
| 6 | // use 'service()' for JS class object API |
| 7 | // use 'provider()' for configurable service API |
| 8 | |
| 9 | |
| 10 | // this is a service definition |
| 11 | function ItemServiceTwo() { |
| 12 | var items = [ |
| 13 | {id: 0, label: 'Item 0'}, |
| 14 | {id: 1, label: 'Item 1'} |
| 15 | ]; |
| 16 | this.list = function () { |
| 17 | return items; |
| 18 | }; |
| 19 | this.add = function (item) { |
| 20 | items.push(item); |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | // this is a provider definition |
| 25 | function ItemServiceThree(optItems) { |
| 26 | var items = optItems || []; |
| 27 | |
| 28 | this.list = function () { |
| 29 | return items; |
| 30 | }; |
| 31 | this.add = function (item) { |
| 32 | items.push(item); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | angular.module('notesApp', []) |
| 37 | |
| 38 | // [provider] define item service as configurable provider |
| 39 | .provider('ItemServiceThree', function () { |
| 40 | var haveDefaultItems = true; |
| 41 | |
| 42 | this.disableDefaultItems = function () { |
| 43 | haveDefaultItems = false; |
| 44 | }; |
| 45 | |
| 46 | // this function gets our dependencies.. |
| 47 | this.$get = [function () { |
| 48 | var optItems = []; |
| 49 | if (haveDefaultItems) { |
| 50 | optItems = [ |
| 51 | {id: 0, label: 'Item 0'}, |
| 52 | {id: 1, label: 'Item 1'} |
| 53 | ]; |
| 54 | } |
| 55 | return new ItemServiceThree(optItems); |
| 56 | }]; |
| 57 | }) |
| 58 | |
| 59 | // [provider] define configuration for provider |
| 60 | .config(['ItemServiceThreeProvider', function (ItemServiceThreeProvider) { |
| 61 | // to see how the provider can change configuration |
| 62 | // change the value of shouldHaveDefaults to true and |
| 63 | // try running the example |
| 64 | var shouldHaveDefaults = false; |
| 65 | |
| 66 | // get configuration from server. |
| 67 | // set shouldHaveDefaults somehow |
| 68 | // assume it magically changes for now |
| 69 | if (!shouldHaveDefaults) { |
| 70 | ItemServiceThreeProvider.disableDefaultItems(); |
| 71 | } |
| 72 | }]) |
| 73 | |
| 74 | // [service] define item service as a JS class |
| 75 | .service('ItemServiceTwo', [ItemServiceTwo]) |
| 76 | |
| 77 | // [factory] define item service factory |
| 78 | .factory('ItemService', [function () { |
| 79 | var items = [ |
| 80 | {id: 0, label: 'Item 0'}, |
| 81 | {id: 1, label: 'Item 1'} |
| 82 | ]; |
| 83 | return { |
| 84 | list: function () { |
| 85 | return items; |
| 86 | }, |
| 87 | add: function (item) { |
| 88 | items.push(item); |
| 89 | } |
| 90 | }; |
| 91 | }]) |
| 92 | |
| 93 | // ====================================================================== |
| 94 | // define controllers... |
| 95 | .controller('MainCtrl', [function () { |
| 96 | var self = this; |
| 97 | self.tab = 'first'; |
| 98 | self.open = function (tab) { |
| 99 | self.tab = tab; |
| 100 | }; |
| 101 | }]) |
| 102 | |
| 103 | .controller('SubCtrl', ['ItemService', function (ItemService) { |
| 104 | var self = this; |
| 105 | self.list = function () { |
| 106 | return ItemService.list(); |
| 107 | }; |
| 108 | self.add = function () { |
| 109 | var n = self.list().length; |
| 110 | ItemService.add({ |
| 111 | id: n, |
| 112 | label: 'Item ' + n |
| 113 | }); |
| 114 | }; |
| 115 | }]); |