Hi Brandon,
Yes, you can get a ref to the root by using the firebase service:
firebase: Ember.inject.service(),
someMethod() {
const ref = this.get('firebase');
}
As for the initial question, if you were happy to move the "array" location down a few levels in the firebase DB, you could use a singleton EmberFire model:
// app/models/thing-collection.js
export default Ember.Model.extend({
activeThings: DS.hasMany('thing', {async: true})
});
Then you would always fetch the singleton thingCollection with id '1' and this can be your source of truth:
model() {
return this.store.findRecord('thingCollection', '1');
},
actions: {
someAction() {
this.store.createRecord('thing', {x: 'y'})
.save()
.then((r) => {
const model = this.get('model');
model.get('activeThings').pushObject(r);
return model.save();
});
}
}
If you really don't want to do that, you could use a raw ref and listen to the 'child_added', 'child_removed' events and pump this into an Ember array manually:
const activeThings = Ember.A();
ref.on('child_added', (snap) => {
const id = snap.key();
this.store.findRecord('thing', id).then(t => activeThings.pushObject(t));
});
ref.on('child_removed', (snap) => {
const id = snap.key();
// if peekRecord finds it, no need to findRecord again
const thing = this.store.peekRecord('thing', id);
if (thing) {
activeThings.removeObject(t);
}
});
Hope that gives you some ideas to work with,
🔥
Tim