HAPI Developer's Guide
Previous Topic  Next Topic 


Note: for a full guide, visit the website: hapi.heuristscholar.org


Overview


The Heurist Application Programming Interface (HAPI) is a Javascript library that provides access to the Heurist database, on which the Heurist web application is built.  Because of the flexible nature of the Heurist DB structure, it is able to support a wide range of applications.  HAPI makes development of such AJAX applications quick and easy.


If there is not an an example of how to do a certain thing in the Useful snippets section of this document, the API reference will most likely have what you are looking for.  The naming of functions and classes is clear enough that usually one can easily discover how to do something.


Hello, world

This is as near as one might come to a "Hello world" application - a basic search page.


    <html>

     <head>

      <title>HAPI Search sample application</title>


      <script src=http://hapi.heuristscholar.org/load?v=02&;key=ce1c992c139cfd5a6fdc77c5c71444925f229942></script>


Loads the HAPI library.  The v parameter specifies the version.  If omitted it will default to the latest stable version.  If a value of d is given, the latest development version of HAPI will be loaded.  The key parameter is the API key for your site.


      <script>

        function doSearch() {

        var mysearch = new HSearch(document.getElementById("search-string").value);


            var loader = new HLoader( 

                function(s,r) {

                    displayResults(r);

                },

                function(s,e) {

                    alert("load failed: " + e);

                });

            HeuristScholarDB.loadRecords(mysearch, loader);

        }


        function displayResults(r) {

            var div = document.getElementById("results");

            div.innerHTML = "";

            for (var i = 0; i < r.length; i++) {

                div.innerHTML += "<a href=\"http://heuristscholar.org/resource/" + r[i].getID() + "\">" + r[i].getTitle() + "</a><br/>";

           }

        }

      </script>

     </head>


The HSearch object specifies a Heurist search.  The syntax is the same as that used in the Heurist web application search.

An HLoader contains an onLoad callback and an onError callback.

HeuristScholarDB, an instance of an HStorageManager, provides access to the main instance of the Heurist DB.


     <body>

      <h3>Search Heurist database</h3>

      <input type="text" id="search-string"></input>

      <input type="button" value="search" onclick="doSearch();"></input>

      <div id="results"></div>

     </body>

    </html>


Click here to try this sample application.




Useful snippets



Create a new record


    var record = new HRecord();



Set record type


    record.setRecordType(HRecordTypeManager.getRecordTypeById(1));



Get detail types for record


    var detailTypes = HDetailManager.getDetailTypesForRecordType(record.getRecordType());



Add a detail to a record


    var TitleType = HDetailManager.getDetailTypeById(160);

    record.addDetail(TitleType, "HAPI test record");



Save a record


    var saver = new HSaver(

        function(r) { alert( ":-)" ); },

        function(r,e) { alert( ":-(" ); }

    );

    HeuristScholarDB.saveRecord(record, saver);