web-applicationscustom-backend

How to pass data attributes to a FatFractal server extension and access them from extension code?


In a similar vein to a previous question I've asked, I'm wondering A) how to pass individual data attributes to a server extension and B) how to access those data attributes from inside the server extension source code. As a sidebar (and not necessarily the main purpose of this question), is it possible to return an array of FatFractal objects from a server extension? I am using the FatFractal JavaScript SDK in my client application.

Basically, I am creating a simple search utility where the input parameter will be passed to a server extension, and the server extension will return an array of FFUser objects matching the search condition.

FFDL:

CREATE EXTENSION /SearchUsers AS javascript:require('scripts/Search').searchUsers();

Application JS Code:

        function findUsers() {
            if(!ff.loggedIn()) {
                alert("You must be logged in to search for users.");
            }
            else {
                var searchCriteria = $("#input-user-search").val();

                // pass 'searchCriteria' to 'SearchUsers' extension
                // acquire handle to 'matchedUsers' returned from 'SearchUsers'

                ...

            }
        }

Server Extension JS Code:

var ff = require('ffef/FatFractal');

function searchUsers() {

    var matchedUsers; // an array of FFUser objects
    var searchInput; // the search criteria 


    // acquire handle to 'searchCriteria' passed from client app to assign to 'searchInput'

    ...

    // return 'matchedUsers' to client app

}

...

exports.searchUsers = searchUsers;

Again, the commented sections are those which I need some clarification with. The rest of the implementation logic I believe I have figured out. Thanks in advance for any help!


Solution

  • For your application JS code, you can just pass the data as URL query parameters:

    var searchCriteria = $("#input-user-search").val();
    
    // suitable validation and URL encoding ...
    
    ff.getArrayFromExtension("/SeachUsers?search=" + searchCriteria,
        function(result) {
            // success
    });
    

    (Note that you can use the usual getArrayFromUri function too, but you would have to give the full extension URI of `/ff/ext/SearchUsers' in that case.)

    Then in your extension, you need to first get the passed in parameter:

    var searchCriteria = ff.getExtensionRequestData().httpParameters['search'];
    

    Finally, set the result:

    var r = ff.response();
    r.responseCode = 200;
    r.result = matchedUsers;
    

    Note that you could also use the postObjToExtension function and, well, post an object to your extension, but that might be overkill for this scenario.