Testing viewingSelf from plugin outlet

I’m using the user-profile-primary plugin outlet to add some things to the user pages.

In the .hbs template, I want some things to be conditional on whether the displayed user is the logged on user or someone else. This is done via a computed viewingSelf property in the internal templates, but it’s not available directly from the user model.

From Important changes to Plugin Outlets for Ember 2.10 I can see that the child template no longer automatically inherits everything the parent template could use, and since viewingSelf is a computed property it looks like it is one of the missing things.

I also see in there that I can make a connector, and add whatever I need, so I’m trying something like this, just trying to get it so {{newTestThing}} will print the two usernames first:

export default {
	setupComponent(args, component) {
		const modelUser = args.model.get("username");
		const activeUser = ???;
		component.set('newTestThing', modelUser + " --?-- " + activeUser);
	}
}

I can’t work out what should go into ??? though.

currentUser, application, etc. don’t seem to exist on this or on any of the objects contained in args (at least from trying various combinations, then setting a breakpoint and looking around in Chrome’s js debugger). [EDIT: This statement is wrong. this.currentUser works fine. See a few posts down.]

I’m probably missing something simple, but can’t see the wood for the trees at the moment. Any help much appreciated!

(I’m not tied to this method of doing things, either. If there’s an easier way to say {{#if viewingSelf}} inside the user-profile-primary outlet then that’s my real goal.)

I thought for the most part a lot if the “main” objects had global scope, but you may need to pass the Discourse object to the function.

For currentUser try something like

var username = Discourse.User.currentProp('username') || "anon";
2 Likes

That did the trick. Many thanks for helping so quickly!

I don’t recommend using the global objects. We are trying to phase those out because they are essentially global variables.

Instead you should be able to do something like:

const username = this.currentUser ? this.currentUser.get('username') : 'anon';
5 Likes

I can confirm that works, including in the file I was having problems with.

My statement that “this.currentUser” didn’t exist within the connector was wrong.

Looking again, I see where I went wrong: I was using Chrome’s debugger to see which properties/methods existed on the “this” object, but I didn’t realise it only shows some of them at the top level, with others nested inside the _proto_ level. I’m still very much a javascript newbie and still getting surprised by differences vs my bread & butter C++.

Many thanks for your help!

1 Like