One option would be to use a combination of ember-truth-helpers. Maybe something like:
{{#if (and (not model.someAsyncRelationship.someValue) model.someAsyncRelationship.isFullfilled)}}
// do something
{{/if}}
But that’s kind of ugly.
Another option, if you find you have this pattern a lot, is to build your own custom helper. For Example:
// app/helpers/async-not.js
import Ember from 'ember';
const { get } = Ember;
export default Ember.Helper.extend(function([isFulfilled, value], hash) {
return isFulfilled && !value;
});
Using it:
{{#if (async-not model.someAsyncRelationship.isFulfilled model.someAsyncRelationship.someValue)}}
// do something
{{/if}}
That still kind of leaves something to be desired though. That’s quite verbose.
Okay I think perhaps I blew the scope now, but here is a helper that tries to slim down the surface of it’s API.
// app/helpers/async-value.js
import Ember from 'ember';
const {
get,
addObserver,
removeObserver
} = Ember;
export default Ember.Helper.extend({
installObserver(relationship, key) {
if(this._installObsRelationship !== relationship || this._installObsKey !== key) {
this.uninstallObserver();
this._installObsRelationship = relationship;
this._installObsKey = key;
addObserver(this._installObsRelationship, this._installObsKey, this, this.recompute);
addObserver(this._installObsRelationship, 'isFulfilled', this, this.recompute);
}
},
uninstallObserver() {
if(this._installObsRelationship && this._installObsKey) {
removeObserver(this._installObsRelationship, this._installObsKey, this, this.recompute);
removeObserver(this._installObsRelationship, 'isFulfilled', this, this.recompute);
this._installObsRelationship = null;
this._installObsKey = null;
}
},
willDestroy() {
this.uninstallObserver();
},
compute([asyncRelationship, key], hash) {
this.installObserver(asyncRelationship, key);
return asyncRelationship && !get(asyncRelationship, key) && get(asyncRelationship, 'isFulfilled');
}
});
Using it:
{{#if (async-not model.someAsyncRelationship 'someValue')}}
// do something
{{/if}}
In this case, we have to manually manage some observers for dynamic values. This is something that HTMLBars / Ember do for us automatically when we use full paths (model.asyncRelationship.someValue
), however since we want to reduce the redundancy of providing two full paths, we have to manually manage two observers. Seeing that should make you really appreciate Ember.
Using observers in this way might not be the “recommended solution”. But if you have this pattern a lot in your app, it’s what I would do.
I made an ember-twiddle to help demonstrate this: Ember Twiddle