JavaScript Interop (Part 2)
JavaScript To .NET
There are three ways of
doing a callback from JavaScript to .NET code:
• Static .NET method call
• Instance method call
• Component instance
method call
Static .NET Method Call
To invoke a .NET function
using JavaScript, we should declare the function as static and apply the JSInvokable
attribute to the method.
Instance Method Call
This method is a little
bit tricky. We need to pass an instance of the .NET object to be able to call
it. First, we need a class that will handle the method call. This class accepts
a string (a name) as an argument in its constructor, along with a method named SayHello
that produces a string comprising “Hello,” followed by the name provided
during the instance creation.
We should start by
creating an instance of that class, providing it with a name, and generating a DotNetObjectReference<T>
to enable JavaScript to interact with the instance. However, we first need the
JavaScript code that can invoke the .NET method. There are additional methods
available for invoking .NET functions from JavaScript. One approach involves
calling a method on a component instance to trigger an action, although this is
not considered a best practice for Blazor Server. Alternatively, we can invoke
a method on a component instance through the use of a helper class.
Implementing An Existing
JavaScript Library
The optimal strategy is
to refrain from porting JavaScript libraries. Blazor must maintain
synchronization between the DOM and the render tree, and allowing JavaScript to
alter the DOM can threaten that harmony.
Many component providers,
including Telerik, Syncfusion, Radzen, and Blazm, offer native components. This
means that their components are not mere wrappers around a JavaScript library
but are designed specifically for Blazor using C#. While these components may
utilize JavaScript to some extent, the objective is to limit that usage as much
as possible.
Certain components may
not be capable of utilizing JavaScript implementations as they require direct
manipulation of the DOM. Blazor effectively manages the synchronization between
the DOM and the render tree; however, it's best to refrain from direct DOM
manipulation. If there's a need to incorporate JavaScript for a specific task,
ensure that you place a tag outside the area being manipulated. Blazor will
monitor that tag and will disregard everything contained within it.
We have explored how to
integrate a JavaScript library with Blazor, making this a viable option if
necessary. The vendors of components are committing resources to Blazor, so
it's likely that they offer what we require, which means we may not need to
spend time developing our own component library.
Conclusion
We learned about calling
JavaScript from .NET as well as calling .NET from JavaScript. Generally, we
won’t need to do JavaScript calls, as the Blazor community or component vendors
have solved the problem for us. We also looked at how we can port an existing
library if required.
Comments
Post a Comment