Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<div style="margin-top: 2em">
<button @onclick="CreateInstanceByConstructorFunction">Call constructor function InvokeNewAsync</button>
<button @onclick="CreateInstanceByClassConstructor">Call class constructor with InvokeNewAsync</button>
<button @onclick="ChangeInstanceMethodWithFunctionReference">Change instance method with function reference</button>
<span>@InstanceMessage</span>
</div>

Expand Down Expand Up @@ -146,13 +147,13 @@

private async Task CreateInstanceByConstructorFunction()
{
var dogRef = await JSRuntime.InvokeNewAsync("Dog", ["Igor"]);
var dogRef = await JSRuntime.InvokeNewAsync("Dog", "A dog");
InstanceMessage = await dogRef.InvokeAsync<string>("bark");
}

private async Task CreateInstanceByClassConstructor()
{
var catRef = await JSRuntime.InvokeNewAsync("Cat", ["Whiskers"]);
var catRef = await JSRuntime.InvokeNewAsync("Cat", "A cat");
InstanceMessage = await catRef.InvokeAsync<string>("meow");
}

Expand All @@ -166,6 +167,15 @@
await JSRuntime.SetValueAsync<int>("testObject.getOnlyProperty", 123);
}

private async Task ChangeInstanceMethodWithFunctionReference()
{
var dogRef = await JSRuntime.InvokeNewAsync("Dog", "A dog");
var dogFuncRef = await dogRef.GetValueAsync<IJSObjectReference>("bark");
var catRef = await JSRuntime.InvokeNewAsync("Cat", "A cat");
await catRef.SetValueAsync("meow", dogFuncRef);
InstanceMessage = await catRef.InvokeAsync<string>("meow");
}

class TestObjectModel
{
public int Num { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export module DotNet {
* @throws Error if the given value is not an Object.
*/
export function createJSObjectReference(jsObject: any): any {
if (jsObject && typeof jsObject === "object") {
if (jsObject && (typeof jsObject === "object" || jsObject instanceof Function)) {
cachedJSObjectsById[nextJsObjectId] = new JSObject(jsObject);

const result = {
Expand Down
Loading