Javascript auto-complete/refactoring for JSON generated from Java classes

When writing javascript, I would like access to the Java backing classes for the JSON generated from instances of those Java classes.   For example, in Freemarker it is possible to put a specially formatted comment at the top of the file that maps variable names to java classes.  This allows the Freemarker editor to do auto-completion using the backing Java classes and more importantly, allow refactoring of the Java class and have it reflected in the Freemarker file.   I would like to do the same in a html/javascript file.  Is there a way to add a "comment" at the top of one of these files that would allowing mapping to Java classes?

7 comments
Comment actions Permalink

In fact this should work without any additional comments. If your JSON file contains fully-qualified class names, renaming a class will find the references in JSON and offer you to update them. Also you can use the Ctrl-Alt-Space keyboard shortcut to complete qualified class names in any context.

0
Comment actions Permalink

Thanks for the reply!  Could you explain a bit more by what you mean by a "JSON file"?   The JSON is dynamically built at runtime and so does not exist while editing the html/javascript file.

0
Comment actions Permalink

Sorry, I didn't read your initial post attentively enough. What I said about JSON also applies to the JavaScript code that generates it.

0
Comment actions Permalink

Sorry, I'm not being very clear.  The server is written in Java.   When it returns html/javascript to the browser it includes JSON that is dynamically built from Java objects.  The JavaScript in the browser uses that JSON to populate a html select (for example).  What I'd like is for the IDEA javascript/html editor to be aware of the not-yet-generated JSON based on the Java classes that do exist. Does that help make my question clearer?

0
Comment actions Permalink

Not completely. Could you please show a code snippet where you expect completion/refactoring to work?

0
Comment actions Permalink

Here's an example.   We are using handlebars.js to do client side templates, so the html has in it handlebar helpers.   When the helpers execute they have the dynamic JSON in their context, but that doesn't help during editing of the html.  In the example below it would be nice to associate the JSON "promotionContract" with the Java class PromotionContract.   

Java class:

public class PromotionContract implements Serializable {
    private String _loyaltyProgramCode;
    private List<LoyaltyPaymentPartner> _paymentPartnerList = new ArrayList<LoyaltyPaymentPartner>();
}


JSON dynamically built at runtime:


{
"promotionContract":{"loyaltyProgramCode":"special_loyalty",
"paymentPartnerList":[ {"cogInvoiceEmailAddresses":null,"createdTimestamp":null,"loyaltyPaymentPartnerId":0,"memberLoginCallbackUrl":null,"partnerCode":"rewardspay_dev","partnerName":null,"securityTokenValidationUrl":null,"unauthenticatedUserTimeoutInMinutes":0,"unlinkUserAccountUrl":null}
]
}


HTML file with handlebars.js:

       <tr>
            <td>Loyalty Program</td>
            <td>
                <select name="loyalty_program_code">
                   {{#option_map  promotionContract.loyaltyProgramCode ""}} {{/option_map}}
                </select>
            </td>
        </tr>

        <tr>
            <td>Payment Partners</td>
            <td>
                <select name="payment_partner_codes" multiple>
                    {{#option_map  promotionContract.paymentPartnerList.[0].partnerCode}} {{/option_map}}
                </select>
            </td>
        </tr>
0
Comment actions Permalink

Yes! That would be very handy.
My own application uses JAX-RS on the server and ExtJS on the client.
I think IDEA cannot reliably associate arbitrary js objects with Java objects by itself, but a JS annotation would be nice that allows me to configure the corresponding java class myself!

Of course IDEA _could_ parse the url passed into Ext.Ajax.request, find the corresponding JAX-RS resource class and method, look at the return type and associate it with the proper parameter of the 'success' handler passed into Ext.Ajax.request ;-)

0

Please sign in to leave a comment.