├── .gitignore ├── README.md ├── changelog.md ├── bower.json ├── package.json ├── test.html ├── LICENSE └── ionic-core.js /.gitignore: -------------------------------------------------------------------------------- 1 | .*.sw* 2 | *~ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NOTICE: 2 | 3 | This library has been deprecated by the [ionic-platform-web-client](https://github.com/driftyco/ionic-platform-web-client). 4 | 5 | Head over to our [docs](https://docs.ionic.io) to learn more about setting up a project with the Ionic Platform. 6 | 7 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | ## 0.1.0 5 | 6 | * removed hardcoded references 7 | 8 | ## 0.1.9 9 | 10 | * Fixed a bug in the `$ionicUser` injections. 11 | 12 | ## 0.1.8 13 | 14 | * Added $ionicCoreSettings, which gets auto-generated by the new config feature in the CLI 15 | * Updated $ionicUser to load app ID from $ionicCoreSettings 16 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ionic-service-core", 3 | "version": "0.1.10", 4 | "homepage": "http://ionic.io", 5 | "authors": [ 6 | "Eric Bobbitt ", 8 | "William Pelrine " 9 | ], 10 | "description": "Core features for the Ionic Platform", 11 | "main": "ionic-core.js", 12 | "license": "Apache2", 13 | "ignore": [ 14 | "**/.*", 15 | "node_modules", 16 | "bower_components", 17 | "test", 18 | "tests" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ionic-service-core", 3 | "version": "0.1.10", 4 | "description": "Ionic Core Component", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+https://github.com/driftyco/ionic-service-core.git" 8 | }, 9 | "keywords": [ 10 | "Ionic", 11 | "core" 12 | ], 13 | "author": "Ionic", 14 | "contributors": [ 15 | "Eric Bobbitt ", 16 | "William Pelrine " 17 | ], 18 | "license": "Apache 2.0", 19 | "bugs": { 20 | "url": "https://github.com/driftyco/ionic-service-core/issues" 21 | }, 22 | "homepage": "https://github.com/driftyco/ionic-service-core#readme" 23 | } 24 | -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test 5 | 6 | 7 | 8 | 9 | 10 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /ionic-core.js: -------------------------------------------------------------------------------- 1 | angular.module('ionic.service.core', []) 2 | /** 3 | * @private 4 | * Provides a safe interface to store objects in persistent memory 5 | */ 6 | .provider('persistentStorage', function() { 7 | return { 8 | $get: ['$q', '$window', function($q, $window) { 9 | var objectCache = {}; 10 | var memoryLocks = {}; 11 | 12 | var persistenceStrategy = { 13 | get: function(key) { 14 | return $window.localStorage.getItem(key); 15 | }, 16 | remove: function(key) { 17 | return $window.localStorage.removeItem(key); 18 | }, 19 | set: function(key, value) { 20 | return $window.localStorage.setItem(key, value); 21 | } 22 | }; 23 | 24 | return { 25 | /** 26 | * Stores an object in local storage under the given key 27 | */ 28 | storeObject: function(key, object) { 29 | 30 | // Convert object to JSON and store in localStorage 31 | var json = JSON.stringify(object); 32 | persistenceStrategy.set(key, json); 33 | 34 | // Then store it in the object cache 35 | objectCache[key] = object; 36 | }, 37 | 38 | /** 39 | * Either retrieves the cached copy of an object, 40 | * or the object itself from localStorage. 41 | * Returns null if the object couldn't be found. 42 | */ 43 | retrieveObject: function(key) { 44 | 45 | // First check to see if it's the object cache 46 | var cached = objectCache[key]; 47 | if (cached) { 48 | return cached; 49 | } 50 | 51 | // Deserialize the object from JSON 52 | var json = persistenceStrategy.get(key); 53 | 54 | // null or undefined --> return null. 55 | if (json == null) { 56 | return null; 57 | } 58 | 59 | try { 60 | return JSON.parse(json); 61 | } catch (err) { 62 | return null; 63 | } 64 | }, 65 | 66 | /** 67 | * Locks the async call represented by the given promise and lock key. 68 | * Only one asyncFunction given by the lockKey can be running at any time. 69 | * 70 | * @param lockKey should be a string representing the name of this async call. 71 | * This is required for persistence. 72 | * @param asyncFunction Returns a promise of the async call. 73 | * @returns A new promise, identical to the one returned by asyncFunction, 74 | * but with two new errors: 'in_progress', and 'last_call_interrupted'. 75 | */ 76 | lockedAsyncCall: function(lockKey, asyncFunction) { 77 | 78 | var deferred = $q.defer(); 79 | 80 | // If the memory lock is set, error out. 81 | if (memoryLocks[lockKey]) { 82 | deferred.reject('in_progress'); 83 | return deferred.promise; 84 | } 85 | 86 | // If there is a stored lock but no memory lock, flag a persistence error 87 | if (persistenceStrategy.get(lockKey) === 'locked') { 88 | deferred.reject('last_call_interrupted'); 89 | deferred.promise.then(null, function() { 90 | persistenceStrategy.remove(lockKey); 91 | }); 92 | return deferred.promise; 93 | } 94 | 95 | // Set stored and memory locks 96 | memoryLocks[lockKey] = true; 97 | persistenceStrategy.set(lockKey, 'locked'); 98 | 99 | // Perform the async operation 100 | asyncFunction().then(function(successData) { 101 | deferred.resolve(successData); 102 | 103 | // Remove stored and memory locks 104 | delete memoryLocks[lockKey]; 105 | persistenceStrategy.remove(lockKey); 106 | }, function(errorData) { 107 | deferred.reject(errorData); 108 | 109 | // Remove stored and memory locks 110 | delete memoryLocks[lockKey]; 111 | persistenceStrategy.remove(lockKey); 112 | }, function(notifyData) { 113 | deferred.notify(notifyData); 114 | }); 115 | 116 | return deferred.promise; 117 | } 118 | }; 119 | }] 120 | }; 121 | }) 122 | 123 | /** 124 | * A core Ionic account identity provider. 125 | * 126 | * Usage: 127 | * angular.module('myApp', ['ionic', 'ionic.service.core']) 128 | * .config(['$ionicAppProvider', function($ionicAccountProvider) { 129 | * $ionicAppProvider.identify({ 130 | * app_id: 'x34dfxjydi23dx' 131 | * }); 132 | * }]); 133 | */ 134 | .provider('$ionicApp', ['$httpProvider', function($httpProvider) { 135 | var app = {}; 136 | 137 | var settings = { 138 | 'api_server': 'https://apps.ionic.io', 139 | 'push_api_server': 'https://push.ionic.io', 140 | 'analytics_api_server': 'https://analytics.ionic.io' 141 | }; 142 | 143 | var _is_cordova_available = function() { 144 | 145 | console.log('Ionic Core: searching for cordova.js'); 146 | 147 | try { 148 | if (window.cordova || cordova) { 149 | console.log('Ionic Core: cordova.js has already been loaded'); 150 | return true; 151 | } 152 | } catch(e) {} 153 | 154 | var scripts = document.getElementsByTagName('script'); 155 | var len = scripts.length; 156 | for(var i = 0; i < len; i++) { 157 | var script = scripts[i].getAttribute('src'); 158 | if(script) { 159 | var parts = script.split('/'); 160 | var partsLength = 0; 161 | try { 162 | partsLength = parts.length; 163 | if (parts[partsLength-1] === 'cordova.js') { 164 | console.log('Ionic Core: cordova.js has previously been included.'); 165 | return true; 166 | } 167 | } catch(e) {} 168 | } 169 | } 170 | 171 | return false; 172 | }; 173 | 174 | this.identify = function(opts) { 175 | if (!opts.gcm_id){ 176 | opts.gcm_id = 'None'; 177 | } 178 | app = opts; 179 | }; 180 | 181 | /** 182 | * Set a config property. 183 | */ 184 | this.set = function(k, v) { 185 | settings[k] = v; 186 | }; 187 | 188 | this.setApiServer = function(server) { 189 | settings.api_server = server; 190 | }; 191 | 192 | this.$get = [function() { 193 | return { 194 | getId: function() { 195 | return app.app_id; 196 | }, 197 | getGcmId: function(){ 198 | return app.gcm_id; 199 | }, 200 | getValue: function(k) { 201 | return settings[k]; 202 | }, 203 | getApiUrl: function() { 204 | return this.getValue('api_server'); 205 | }, 206 | getApiKey: function() { 207 | return app.api_key; 208 | }, 209 | getApiEndpoint: function(service) { 210 | var app = this.getApp(); 211 | if(!app) return null; 212 | 213 | return this.getApiUrl() + '/api/v1/' + app.app_id + '/' + service; 214 | }, 215 | 216 | /** 217 | * Get the registered app for all commands. 218 | */ 219 | getApp: function() { 220 | return app; 221 | }, 222 | 223 | getDeviceTypeByNavigator: function() { 224 | return (navigator.userAgent.match(/iPad/i)) == "iPad" ? "ipad" : (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "iphone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "blackberry" : "unknown"; 225 | }, 226 | 227 | loadCordova: function() { 228 | if(!_is_cordova_available()) { 229 | var cordova_script = document.createElement('script'); 230 | var cordova_src = 'cordova.js'; 231 | switch(this.getDeviceTypeByNavigator()) { 232 | case 'android': 233 | if (window.location.href.substring(0, 4) === "file") { 234 | cordova_src = 'file:///android_asset/www/cordova.js'; 235 | } 236 | break; 237 | 238 | case 'ipad': 239 | case 'iphone': 240 | try { 241 | var resource = window.location.search.match(/cordova_js_bootstrap_resource=(.*?)(&|#|$)/i); 242 | if (resource) { 243 | cordova_src = decodeURI(resource[1]); 244 | } 245 | } catch(e) { 246 | console.log('Could not find cordova_js_bootstrap_resource query param'); 247 | console.log(e); 248 | } 249 | break; 250 | 251 | case 'unknown': 252 | return false; 253 | 254 | default: 255 | break; 256 | } 257 | cordova_script.setAttribute('src', cordova_src); 258 | document.head.appendChild(cordova_script); 259 | console.log('Ionic Core: injecting cordova.js'); 260 | } 261 | }, 262 | 263 | bootstrap: function() { 264 | this.loadCordova(); 265 | } 266 | } 267 | }]; 268 | }]) 269 | 270 | /** 271 | * @ngdoc service 272 | * @name $ionicUser 273 | * @module ionic.service.core 274 | * @description 275 | * 276 | * An interface for storing data to a user object which will be sent with many ionic services 277 | * 278 | * Add tracking data to the user by passing objects in to the identify function. 279 | * The _id property identifies the user on this device and cannot be overwritten. 280 | * 281 | * @usage 282 | * ```javascript 283 | * $ionicUser.get(); 284 | * 285 | * // Add info to user object 286 | * $ionicUser.identify({ 287 | * username: "Timmy" 288 | * }); 289 | * 290 | * ``` 291 | */ 292 | .factory('$ionicUser', [ 293 | '$q', 294 | '$ionicCoreSettings', 295 | '$timeout', 296 | '$http', 297 | 'persistentStorage', 298 | '$ionicApp', 299 | function($q, $ionicCoreSettings, $timeout, $http, persistentStorage, $ionicApp) { 300 | // User object we'll use to store all our user info 301 | 302 | 303 | var storageKeyName = 'ionic_analytics_user_' + $ionicApp.getApp().app_id, 304 | user = persistentStorage.retrieveObject(storageKeyName) || {}, 305 | deviceCordova = ionic.Platform.device(), 306 | device = { 307 | screen_width: window.innerWidth * (window.devicePixelRatio || 1), 308 | screen_height: window.innerHeight * (window.devicePixelRatio || 1) 309 | }; 310 | 311 | if (deviceCordova.model) device.model = deviceCordova.model; 312 | if (deviceCordova.platform) device.platform = deviceCordova.platform; 313 | if (deviceCordova.version) device.version = deviceCordova.version; 314 | if (deviceCordova.uuid) device.uuid = deviceCordova.uuid; 315 | 316 | // Flag if we've changed anything on our user 317 | var dirty = false; 318 | dirty = storeOrDirty('is_on_device', ionic.Platform.isWebView()); 319 | dirty = storeOrDirty('device', device); 320 | if (!user._id) { 321 | user._id = generateGuid(); 322 | dirty = true; 323 | } 324 | 325 | if (dirty) { 326 | persistentStorage.storeObject(storageKeyName, user); 327 | } 328 | 329 | function generateGuid() { 330 | // Some crazy bit-twiddling to generate a random guid 331 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { 332 | var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); 333 | return v.toString(16); 334 | }); 335 | } 336 | 337 | function storeOrDirty(key, value) { 338 | // Store the key on the user object and return whether something changed 339 | if (!angular.equals(user[key], value)) { 340 | user[key] = value; 341 | return true; 342 | } 343 | return false; 344 | } 345 | 346 | return { 347 | /** 348 | * Push a value to the array with the given key. 349 | * @param key the key 350 | * @param value the value 351 | * @param isUnique whether to only push if it doesn't exist in the set 352 | * 353 | */ 354 | 355 | _op: function(key, value, type) { 356 | var u = user.user_id; 357 | var appId = ''; 358 | if ($ionicCoreSettings.get('app_id')) { 359 | appId = $ionicCoreSettings.get('app_id') 360 | } else { 361 | appId = $ionicApp.getId(); 362 | } 363 | if(!u) { 364 | throw new Error("Please call identify with a user_id before calling push"); 365 | } 366 | var o = {}; 367 | o['user_id'] = u; 368 | o[key] = value; 369 | 370 | return $http.post($ionicApp.getApiUrl() + '/api/v1/app/' + appId + '/users/' + type, o); 371 | }, 372 | /** 373 | * Push the given value into the array field identified by the key. 374 | * Pass true to isUnique to only push the value if the value does not 375 | * already exist in the array. 376 | */ 377 | push: function(key, value, isUnique) { 378 | if(isUnique) { 379 | return this._op(key, value, 'pushUnique'); 380 | } else { 381 | return this._op(key, value, 'push'); 382 | } 383 | }, 384 | /** 385 | * Pull a given value out of the array identified by key. 386 | */ 387 | pull: function(key, value) { 388 | return this._op(key, value, 'pull'); 389 | }, 390 | /** 391 | * Set the given value under the key in the user. This overwrites 392 | * any other data under that field. To append data to list, use push above. 393 | */ 394 | set: function(key, value) { 395 | return this._op(key, value, 'set'); 396 | }, 397 | /** 398 | * Remove the field for the given key. 399 | */ 400 | unset: function(key) { 401 | return this._op(key, '', 'unset'); 402 | }, 403 | generateGUID: function() { 404 | return generateGuid(); 405 | }, 406 | identify: function(userData) { 407 | var appId = ''; 408 | if ($ionicCoreSettings.get('app_id')) { 409 | appId = $ionicCoreSettings.get('app_id') 410 | } else { 411 | appId = $ionicApp.getId(); 412 | } 413 | if (!userData.user_id) { 414 | var msg = 'You must supply a unique user_id field.'; 415 | throw new Error(msg) 416 | } 417 | 418 | // Copy all the data into our user object 419 | angular.extend(user, userData); 420 | 421 | // Write the user object to our local storage 422 | persistentStorage.storeObject(storageKeyName, user); 423 | 424 | return $http.post($ionicApp.getApiUrl() + '/api/v1/app/' + appId + '/users/identify', userData); 425 | }, 426 | identifyAnonymous: function() { 427 | var appId = ''; 428 | if ($ionicCoreSettings.get('app_id')) { 429 | appId = $ionicCoreSettings.get('app_id') 430 | } else { 431 | appId = $ionicApp.getId(); 432 | } 433 | userData = {}; 434 | userData['user_id'] = generateGuid(); 435 | userData['isAnonymous'] = true; 436 | 437 | // Copy all the data into our user object 438 | angular.extend(user, userData); 439 | 440 | // Write the user object to our local storage 441 | persistentStorage.storeObject(storageKeyName, user); 442 | 443 | return $http.post($ionicApp.getApiUrl() + '/api/v1/app/' + appId + '/users/identify', userData); 444 | }, 445 | get: function() { 446 | return user; 447 | } 448 | } 449 | }]) 450 | 451 | // Auto-generated configuration factory 452 | .factory('$ionicCoreSettings', function() { 453 | var settings = {}; 454 | return { 455 | get: function(setting) { 456 | if (settings[setting]) { 457 | return settings[setting]; 458 | } 459 | return null; 460 | } 461 | } 462 | }) 463 | // Auto-generated configuration factory 464 | 465 | .run(['$ionicApp', function($ionicApp) { 466 | console.log('Ionic Core: init'); 467 | $ionicApp.bootstrap(); 468 | }]); 469 | --------------------------------------------------------------------------------