Is there any way to get a user object by username or id via javascript?
It is difficult to answer without some more context. You can always do this: (try it in the dev tools for Meta)
Discourse.User.findByUsername("David_Taylor").then(function(user){
console.log("User Found", user);
});
But that will result in an additional request to the server, which may not be necessary.
What are you trying to achieve?
Thanks very much. I am trying to get a list of usersnames with certain custom field attributes. As you correctly pointed out, it will result in many additional calls from the API.
Is it possible to achieve what I intend more gracefully?
If this is just for administrators, you might be able to utilise the data-explorer plugin. If you wanted this to work for all users, you would probably need to create a plugin which adds a new HTTP endpoint.
Ich arbeite an einem der Bugs im Zusammenhang mit dem Events-Plugin, bei dem die aktuelle Implementierung auf Benutzernamen basiert. Obwohl dies ein Randfall ist, können sich Benutzernamen ändern, was die Notwendigkeit schafft, einen Benutzer über seine id zu finden.
Ich denke, es sollte eine ähnliche Methode im Kern geben, um einen Benutzer nach seiner ID zu suchen.
Interesse an eurer Meinung dazu.
Ihr Plugin sollte entweder die Benutzernamen-Referenzen aktualisieren können, wenn sich ein Benutzername ändert, oder die erforderlichen Routen und Serialisierer hinzufügen, um die Daten abzurufen.
Ich werde prüfen, ob dies im aktuellen Szenario möglich ist.
Ja. Ich werde eine Methode in UsersController hinzufügen, um einen Benutzer per ID zu finden, und eine Methode im User-Modell auf der Ember-Seite hinzufügen, um diese neue Controller-Methode aufzurufen.