Code Monkey home page Code Monkey logo

Comments (1)

exyi avatar exyi commented on June 25, 2024

Yea, code-only components currently cannot reference and initialize view modules. It is a fundamental issue that view module instances are identified using the JavaScript HTMLElement and found by walking the ancestor chain. Initializing a new view module in the code control would thus break all references to the page view module from inside of any templates the control has. Our current solution is to not support ITemplate in markup controls, and not support view modules in code-only controls.

That being said, you can use JsComponent without view modules. You can register the component globally in any script using dotvvm.registerGlobalComponent. Then reference the script using the RequiredResource

dotvvm.registerGlobalComponent("your-component-name", registerReactControl(... same as in view module $controls))

This will make the component available to all pages and, crucially, to the JsComponent initialized from code. When using a global component in a page, you can add Global property to the component to make sure it isn't overwritten by the module. I'll put this into the docs.


In case that doesn't suit your needs, if you require the view module for some reason, there are two other ways to work around it:

  1. Make your control a markup control. You can put into a library using embedded resources. This will make it impossible a bad idea to add ITemplate/any content properties to the control.
  2. Explicitly use the module from the page. Option A is to simply find the nearest parent with the Internal.ReferencedViewModuleInfoProperty and copy it to the JsComponent:
jsComponent.SetValue(Internal.ReferencedViewModuleInfoProperty,
    this.GetAllAncestors().Select(c => c.GetValue(Internal.ReferencedViewModuleInfoProperty)).First(i => i != null))

Or, if you want to make the code future-proof against us hopefully resolving the core issue, copy the Internal.ReferencedViewModuleInfoProperty from the TreeRoot of the current compilation unit using this (as JsComponent does it):

  [ApplyControlStyle]
  public static void AddReferencedViewModuleInfoProperty(ResolvedControl control)
  {
      if (control.TreeRoot.TryGetProperty(Internal.ReferencedViewModuleInfoProperty, out var x))
      {
          var value = ((ResolvedPropertyValue)x).Value;
          control.SetProperty(new ResolvedPropertyValue(Internal.ReferencedViewModuleInfoProperty, value));
      }
  }

and then access the property by calling this.GetValue(Internal.ReferencedViewModuleInfoProperty) instead of the LINQ.

Same trick by the way applies to using NamedCommands from a code-only control (it's probably more useful for that given there is no global option)

from dotvvm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.