How to access a different model in a connector?

I’m pretty close, but I can’t figure it out. I’ve created a connector here connectors/revision-user-details-after:

In my connector class, I can access args and component via the shouldRender method, which lets me access the current username and the post id:

export default {
  shouldRender(args, component) {
    console.log('postAuthor', args.model.username)
    console.log('postId', args.model.id)
  }
};

I’d also like to get the username of the user who edited the post (in this case, “nachocab”), but args.model is a Post, not a PostRevision.

This is what the history modal looks like:

And the history controller:

How can I access the revision in the connector?

1 Like

Ah, I just realized I can do this!

import Post from "discourse/models/post";

Post.loadRevision(args.model.id, args.model.version).then((result) => {
  const editorUsername = result.username;
  doSomethingElse();
});

UPDATE
@eviltrout I thought I had this figured out, but I just realized that since Post.loadRevision returns a Promise, I can’t put it inside shouldRender():

import Post from 'discourse/models/post';

export default {
  shouldRender(args, component) {
    // returning a promise doesn't work
    return Post.loadRevision(args.model.id, args.model.version).then((revision) => {
      return args.model.username !== revision.username;
    });
  },
};

Is there another way of doing this?

You could move it out of the connector into a new component you insert, and have it show a “Loading…” state until the promise is fulfilled, at which point you set the value and display it.

1 Like

Thanks, Robin. Your tip was just what I needed. :tada:

This works, but I wonder if the direct ajax call is needed (when I took the connector path, I had access to the Post but not the Revision; now that I took the component path, I have access to the Revision, but not the Post :thinking:)

{{! templates/connectors/revision-user-details-after/reply-to-editor.hbs }}
{{reply-to-editor
  postId=model.post_id
  postVersion=model.current_version
  revisionAuthor=model.username
}}
{{! templates/components/reply-to-editor.hbs }}
{{#if isVisible}}
  {{d-button
    action=(action 'replyToEditor')
    label='test'
  }}
{{/if}}
{{! javascripts/discourse/components/reply-to-editor.js.es6 }}
import { ajax } from 'discourse/lib/ajax';

export default Ember.Component.extend({
  isVisible: true,

  didRender() {
    if (!this.get('postId')) return;

    ajax(`/posts/${this.get('postId')}`).then((post) => {
      // only show the button if the revision author is not the post author
      this.set('isVisible', this.get('revisionAuthor') !== post.username);
    });
  }
});

I’m not sure why you don’t have access to the same variables in the template as the connector. It should be identical!