Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

When I actually clicked around on these I noticed some odd behavior (now fixed in the head revision of the project): marking an identifier as preferred doesn't immediately move it to the top of the list on the ajax refresh, but it does move to the top of the list the next time the page is loaded. Basically, there's a bug in the core OpenMRS API where changing the preferred identifier is not reflected in the patient.getaActiveIdentifiers() method until the next time the patient is loaded from the database. I reported this as TRUNK-2188, and I introduced a workaround in the SimplePatient object used indirectly by FragmentUtil.standardPatientObject().

Step 11: Refactoring

Early on in the tutorial, we referred to splitting up our javascript functionality into that which is specific to this instance of the fragment, and that which can be split out into an external resource file. (Any javascript we can put in an external resource file can be cached by the browser, and perhaps minimized by the UI framework, thus speeding up the application over slow internet connections.) Let's go ahead and split that code out.

As we've written things so far, only a single function ("refreshPatientIdentifierTable") can be pulled out. We could rewrite some of the other methods as well, but we'll leave that as an exercise for later. (Key point: javascript that's being moved into a shared resource file must not know about the configuration of a specific instance of the fragment, nor may it reference the fragment's id, or use the 'ui' groovy functions.)

The resource file we build will be cached, but over a satellite internet connection, even checking whether a cached resource has changed can slow down a page, so we're going to combine javascript functions for all of the patient fragments into a single file.

So, let's open "webapp/src/main/webapp/scripts/coreFragments.js", and move our function in there:

Code Block
titlemoving javascript into an external resource

var patientIdentifiersFragment = {

    refreshPatientIdentifierTable: function(divId, patientId) {
        $('#' + divId + '_table > tbody').empty();
        $.getJSON(actionLink('patientIdentifiers', 'getActiveIdentifiers', { returnFormat: "json", patientId: patientId }), function(data) {
            publish(divId + "_table.show-data", data);
        });
    }

}

We've changed two things while moving this function here:

  1. since we're going to be gathering functions for many fragments in this file, we create a "patientIdentifiersFragment" object that contains all functions for our fragment. (Just one, for now.)
  2. we can't call the groovy ui.actionLink method, so we use a javascript function (defined in openmrs.js) instead.

As a result of this we need to change one line in patientIdentifiers.gsp to call this function with the "patientIdentifiersFragment." object prefix:

Code Block

patientIdentifiersFragment.refreshPatientIdentifierTable('${ id }', ${ patient.patientId })