I’ve been looking around at plugin development noticed that in cases such as the discourse translator after retrieving a widget model via findAncestorModel()
there’s a check on whether the result exists before proceeding. I see this happening in the source code too, example:
_getTopicUrl() {
const post = this.findAncestorModel();
return post ? post.get("topic.url") : null;
},
But there are also instances where we don’t check on the existence of the ancestor model, a few lines below:
const post = this.findAncestorModel();
const controller = this.register.lookup("controller:topic");
return post
.get("topic.postStream")
.filterUpwards(this.attrs.id)
.then(() => {
controller.updateQueryParams();
});
How reliable is the existence of the result of findAncestorModel
? Does it depend on the widget we’re in, or is it good practice to always handle a null response even in situations where it seems really unlikely, like in the post-menu