简体   繁体   中英

How to isolate js variables between different instances of the same component?

I have a blazor component that uses fabric js and canvas.

I use classic JS isolation approach, import the module, call the exported js functions, and so on.

MyComponent.razor

<canvas id="@canvasId">
</canvas>

MyComponent.razor.cs

[Inject] private IJSRuntime _jsRuntime { get; init; }
private IJSObjectReference module;
private string canvasId ;

protected override async Task OnInitializedAsync()
{
    canvasId = $"canvas_MediaLabelEditor_{Guid.NewGuid().ToString()}";
    Reload();
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    ...
    string[] args = new string[] { canvasId };
    module = await _jsRuntime.InvokeAsync<IJSObjectReference>("import", "./js/fabricBboxLabel.js");
    await module.InvokeVoidAsync("loadCanvas", args);
}

protected async Task DoAmazingThings()
{
    string[] args = new string[] { ..., ..., ... };
    await module.InvokeVoidAsync("doAmazingThings", args);
}

I use an array to save the current objects drown on the canvas. Then I can move and resize them to finally call a save function that will return all the objects to c# code.

./js/fabricBboxLabel.js

var canvas;
var data; //array
export function loadCanvas(id){
    canvas = new fabric.Canvas(id);
}
export function doAmazingThings(a,b,c){
    array.push( ... )
    ...
    canvas.renderAll();
}

Problem appears when I have more than one instance of this component. All instances seem to be accessing to the same canvas and data variables.

A practical solution would be to define a canvas array or map, but that is not isolation. How could I isolate those variables?


To replicate

Create a component with a button

IsolationTest.razor

<button @onclick="@(async () => await OnClick())">
</button>

IsolationTest.razor.cs

[Inject] private IJSRuntime _jsRuntime { get; init; }
private IJSObjectReference module;
   
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        module = await _jsRuntime.InvokeAsync<IJSObjectReference>("import", "./js/isolationtest.js");
    }
}
public async Task OnClick()
{
   object[] args = new object[] { 5 };
   await module.InvokeVoidAsync("sumar", args);
}

isolationtest.js

var sum=0;

export function sumar(inte) {
    console.log("before sum="+sum)
    sum=sum+inte;
    console.log("after sum="+sum)
}
alert(sum);

Index.razor

<IsolationTest></IsolationTest>
<IsolationTest></IsolationTest>
<IsolationTest></IsolationTest>

output: (when clicking randomly)

before sum=0
after sum=5

before sum=5
after  sum=10

before sum=10
after sum=15

before sum=15
after sum=20

before sum=20
after sum=25

before sum=25
after sum=30

I suggest using array of Sum in you js:

var sum=[];

export function sumar(id,inte) {
    console.log("before sum="+sum[id])
    sum=sum[id]+inte;
    console.log("after sum="+sum[id])
}
alert(sum[id]);

...

[Parameter] public string Id { get;set;}
public async Task OnClick()
{
   object[] args = new object[] { 5 };
   await module.InvokeVoidAsync("sumar",this.Id,args);
}

...

<IsolationTest Id="theFirst"></IsolationTest>
<IsolationTest Id="theSecond"></IsolationTest>
<IsolationTest Id="theThird"></IsolationTest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM