mardi 17 janvier 2017

How can I use a value from one model to shape my store.query() of another?

(My project is using Emberfire, which is Firebase's integration for Ember. This allows me to either make direct database calls through Firebase, or to just use the built-in model functions in Ember)

I have two models. One is /users and contains extra information about a user(particularly their schoolId). The other model is /schools which under each id holds info such as schoolName/etc. I am trying to display the name of a user's school based on the schoolId attached to their /users information.

My current approach is to use a helper called school-name.js in which I am using only Firebase queries to return the value:

import Ember from 'ember';

export default Ember.Helper.extend({
  firebaseApp: Ember.inject.service(),
  compute(uid) {
    //Firebase Database Ref
    const database = this.get('firebaseApp').database();
    //Find the current user's schoolId
    database.ref('/users/' + uid).once('value').then(function(snapshot) {
        var schoolId = snapshot.val().schoolId;

        console.log("schoolId: " + schoolId);
        //Find the current user's schoolName based on their schoolId
        database.ref('/schools/' + schoolId).once('value').then(function(snapshot) {
          var schoolName = snapshot.val().schoolName;
          console.log(schoolName);
          return schoolName;
        });
    });
  }
});

This will console.log the schoolName without a problem, but it will not display it in the template. My intuition tells me that I have to do something with Ember's and Firebase's promises system, however I have no idea where to start and the docs have not been very helpful yet.

My previous attempt was with a mixture of Firebase and Ember's model system.

model: function() {

        //Get current user's uid
        const uid = this.get('session.currentUser.uid');

        //Inject Firebase Auth
        const auth = this.get('firebaseApp').auth();
        var user = auth.currentUser;

        //Inject Firebase Database
        const database = this.get('firebaseApp').database();

        if (user) {
            // User is signed in.
            var schoolIdRef = database.ref('/users/' + uid).once('value').then(function(snapshot) {
                var schoolId = snapshot.val().schoolId;
                console.log(schoolId);
                // ...
            });
            console.log(schoolId);
            return this.store.query('schools', {
                //Query specific school ID
                equalTo: "0" //SET THIS TO schoolId SOMEHOW
            });
        } else {
            // No user is signed in.
        }

    }

However the template would still not load the information. I also made an attempt at this by using only Ember Store calls, however I did not know what to do about the fact that the query would return an array instead of a single value.

Any help is appreciated!




Aucun commentaire:

Enregistrer un commentaire