//
// Service (Factory) for Profiles
//

(function() {
   'use strict';

   angular
     .module('appx')
     .factory('Profiles', Profiles);

   Profiles.$inject = ['$firebaseObject', '$firebaseArray', 'FirebaseUrl'];

   /* @ngInject */
   function Profiles($firebaseObject, $firebaseArray, FirebaseUrl) {
     var profilesRef = new Firebase(FirebaseUrl + 'profiles');
     var profiles = $firebaseArray(profilesRef);

     var service = {
       all: profiles,
       get: get
     };
     return service;

     ////////////////

     /**
      * I've tried a variety of things here, all unsuccesfully typically:
      * 1) No error checking
      * 2) Try/Catch
      * 3) Try/Catch with if(null) checks
      * 4) Try/Catch with if(null) and return {} or null even...
      */
     function get(profileId) {
       try {
         console.log("Getting profile with id: " + profileId);
         var profileRef = profilesRef.child(profileId);
         return $firebaseObject(profileRef);
       } catch(error) {
         console.log("Error: " + error);
       }
     }
   }
})();



//
// Controller
//
(function() {
   'use strict';

   angular
     .module('appx')
     .controller('ProfileCtrl', UserProfileCtrl);

   UserProfileCtrl.$inject = ['auth', 'Users', 'Profiles'];

   /* @ngInject */
   function UserProfileCtrl(auth, Users, Profiles) {
     var vm = this;
     vm.settings = {
       enableFriends: true
     };
     activate();

     ////////////////

     /**
      * I've tried a variety of thigns here, all unsuccesfully typically:
      * 1) Simple set on $scope for user/profile.
      * 2) Resolve on routes to provide user/profile.
      * 3) Resolve on routes to provide user/profile or {} (which makes ends
up yielding bad data in Firebase)
      * 4) Async load in Ctrl watching for $loaded.
      * 5) Async load in Ctrl watching for $loaded as well as testing for
null on user/profile.
      */
     function activate() {
       vm.user = Users.get(auth.uid);
       vm.user.$loaded(function() {
         vm.profile = Profiles.get(vm.user.profileId);
       });
     }
   }
})();

//
// Resolve on routes...
//
// I've returned just the .get()
// I've returned the .loaded()
// I've returned the .loaded OR {}
user: ['Auth', 'Users', function(Auth, Users) {
   var user = Users.get(Auth.$getAuth().uid);
   return user.$loaded();
}],
profile: ['Profiles', 'user', function(Profiles, user) {
   var profile = Profiles.get(user.profileId);
   return profile.$loaded();
}]

//
// View bindings example (nothing special, likely uneccesary)
//
<label class="item item-input">
   <span class="input-label">Display Name</span>
   <input type="text" ng-model="profile.userinfo.displayName">
</label>
<label class="item item-input">
   <span class="input-label">First Name</span>
   <input type="text" ng-model="profile.userinfo.firstName">
</label>


So it's all very simple...

The issue I have is when I attempt to get(ID) and the return is null. None
of my code handles this and shit crashes and burns in every attempt I make.

My goal is to just have the app work gracefully, but I don't know how to
achieve this.

And when the data comes back as null things like the view wont render
properly, buttons / events wont function, etc.
On Tuesday, October 6, 2015 at 11:50:34 AM UTC-6, Vincent Bergeron wrote:

Can you post a sample code of what you're doing?





*From:* firebase...@googlegroups.com <javascript:> [mailto:
firebase...@googlegroups.com <javascript:>] *On Behalf Of *Justin
Martiniak
*Sent:* 6 octobre 2015 13:04
*To:* Firebase + AngularJS
*Subject:* [Firebase + Angular] (Angular) Ionic & Firebase + Best
Practice accessing data which might not exist?



(Sorry if I double posted, ain't ever use google groups)



I've chosen to use Firebase as a simple means to handle authentication and
store my over-the-air data... but I'm running into an issue (occasionally).



I'm using angularFire, so whenever I access data I have the ability to get
a $firebaseObject, $firebaseArray or null.



The null part is what catches me off guard... None of the tutorials or
demos I see go through how to handle the null aspect of the request... they
just go ahead and bind the data to $scope or check if it's $loaded() all
things which wont work with null; and then this cascades into other issues
surfacing and potentially just white/empty views.



So how, or what, is the best way to access data from Firebase that might
not be there?

--
You received this message because you are subscribed to the Google Groups
"Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to firebase-angul...@googlegroups.com <javascript:>.
To post to this group, send email to firebase...@googlegroups.com
<javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/firebase-angular/ff11f31a-fb75-4599-b818-04930c65d2da%40googlegroups.com
<https://groups.google.com/d/msgid/firebase-angular/ff11f31a-fb75-4599-b818-04930c65d2da%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Firebase + AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-angular+unsubscribe@googlegroups.com.
To post to this group, send email to firebase-angular@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-angular/4dc5d0c0-04aa-491f-a5f7-0a502f9d4876%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 9 | next ›
Discussion Overview
groupfirebase-angular @
postedOct 6, '15 at 5:43p
activeOct 6, '15 at 9:33p
posts9
users3

People

Translate

site design / logo © 2021 Grokbase