jQuery UI Widgets Forums General Discussions If jqwidgets.createInstance exists, why don't the docs use it?

This topic contains 4 replies, has 2 voices, and was last updated by  admin 2 years ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author

  • calculuswhiz
    Participant

    I’ve always wondered why the framework uses this as the official way to work with widgets in jQuery:

    
    // Why call some other method to call the method you really want to call?
    $("#grid").jqxGrid('selectrow', 1);
    // What about 0-argument functions? From the coding side, getting a property and calling 0-argument functions are indistinguishable.
    let disabled = $("#grid").jqxGrid('disabled');
    

    After looking through the jqwidgets.d.ts file yesterday, I found out that apparently the framework has methods jqwidgets.createInstance, and each widget has a getInstance method to get an existing instance. This has apparently been around since at least 2016 according to the GitHub Repo.

    So it turns out I can just do this:

    
    const gridInstance = jqwidgets.createInstance('#grid', 'jqxGrid', gridOptions);
    // Or if it exists already... (still not a fan of this, but at least I can declare a special overload in a separate .d.ts file now.)
    const gridInstance = $('#grid').jqxGrid('getInstance');
    // ...
    gridInstance.selectrow(1);
    let disabled = gridInstance.disabled;
    

    which makes so much more sense than using some string as an argument. This also lets you statically check types with the jqwidgets.d.ts file if you use TypeScript.

    I understand legacy support if it hasn’t been always around, but I really feel like the docs need to at least mention this.


    calculuswhiz
    Participant

    …And I just discovered that this is allowed on several different widgets (seems to break for jqxTabs, though…):

    const gridInstance = new jqwidgets.jqxGrid(selector, options);
    

    This is clearly even better and only requires a little tweaking to the .d.ts to get it to work… Shouldn’t that be documented?


    admin
    Keymaster

    Hi calculuswhiz,

    The javascript components use the classic jquery-like syntax. Our framework has several different methods for creating components. We chose to document just one of them. Additionally, the main problem is that setting properties when the instance is used will not update the component. As for the ‘createInstance’ and ‘getInstance’. These are methods which we use internally in our integrations with angular, react, vue. We do not suggest using API which is not in the docs.

    Best regards,
    Peter Stoev

    jQWidgets Team
    https://www.jqwidgets.com/


    calculuswhiz
    Participant

    […] the main problem is that setting properties when the instance is used will not update the component.

    Yes, but isn’t that what setOptions() is for?


    admin
    Keymaster

    Hi calculuswhiz,

    Most people will try instance.propertyName = propertyValue. If you feel like this API is more convenient for you, you can use it in your apps.

    Best regards,
    Peter Stoev

    jQWidgets Team
    https://www.jqwidgets.com/

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.