Search code examples
javascriptweb-servicesrestrequirejsbreeze

requirejs: load script/module from a web service


I have a WebService that query a SQL database. In a sql table, I store some javascript and I want to use it in a webpage using RequireJS.

I try this :

var url = "http://localhost:64952/breeze/app/Objectss?$filter=Id%20eq%201&$select=Script";
require([url], (test) => {
     debugger
     arguments[0];
});

The server respond correctly : https://i.sstatic.net/05lE7.png

But I'm not sure RequireJS is able to load script like this.

I try something else :

var req = breeze.EntityQuery.from("Commands")
          .where("Id", "eq", "1")
          .select("Script");
dataservice.manager.executeQuery(req)
    .then((res : breeze.QueryResult) => {
        if (res.results[0]) {
            require([(<any>res.results[0]).Script], (hekki) => {
                debugger
            });
        }
    });

Doesn't work too...

Do you have any idea to help me please ?!


Solution

  • Create a requirejs plugin responsible for loading dependencies via the breeze api you've put together...

    breezeloader.js:

    define({
        load: function (name, req, onload, config) {
            // load the script using the breeze api you've put together...
            var query = breeze.EntityQuery
                .from("Commands")
                .where("Id", "eq", name)
                .select("Script");
            dataservice.manager.executeQuery(query)
                .then((queryResult: breeze.QueryResult) => {
                    var text = queryResult.results[0].Script;      
    
                    // Have RequireJS execute the JavaScript within
                    //the correct environment/context, and trigger the load
                    //call for this resource.
                    onload.fromText(text);
                });
        }
    });
    

    express dependencies that should be loaded with the breeze loader using the requirejs plugin syntax:

    require(['breezeloader!1', 'jquery', 'foo'], function (hekki, jquery, foo) {
    
        ...
    
    });