Skip to content

Commit

Permalink
Merge pull request #111 from opencomponents/add-before-render
Browse files Browse the repository at this point in the history
add option to pass javascript that executes before render
  • Loading branch information
ricardo-devis-agullo committed Mar 29, 2024
2 parents 32330cc + 62b76f9 commit 947d863
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
8 changes: 6 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ interface TemplateRenderer {
type Template = {
externals: Array<{ global: string | string[]; url: string }>;
};
type CompileOptions = {
interface CompileOptions {
templates?: Record<string, Template> | TemplateRenderer[];
retryInterval?: number;
retryLimit?: number;
};
/**
* JavaScript as a string code to be executed before rendering the templates
*/
beforeRender?: string;
}
type Compiled = { code: string; map: string; dev: string };

declare const ocClient: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oc-client-browser",
"version": "1.7.8",
"version": "1.7.9",
"description": "OC browser client",
"main": "index.js",
"types": "index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/oc-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals define, exports, require, globalThis, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__ */
/* globals define, exports, require, globalThis, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __BEFORE_RENDER__ */
/* eslint no-var: 'off' */
/* eslint prefer-arrow-callback: 'off' */

Expand Down Expand Up @@ -691,6 +691,8 @@ var oc = oc || {};
});
};

__BEFORE_RENDER__;

// render the components
oc.ready(oc.renderUnloadedComponents);

Expand Down
1 change: 1 addition & 0 deletions tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function getFiles({ sync = false, conf = {} }) {
'__REGISTERED_TEMPLATES_PLACEHOLDER__',
JSON.stringify(registeredTemplates)
)
.replaceAll('__BEFORE_RENDER__', conf.beforeRender || '')
.replaceAll('__DEFAULT_RETRY_LIMIT__', conf.retryLimit || 30)
.replaceAll('__DEFAULT_RETRY_INTERVAL__', conf.retryInterval || 5000);

Expand Down
6 changes: 5 additions & 1 deletion test/get-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ describe('oc-client : getData', function () {
describe('when providing the mandatory parameters', function () {
describe('when requesting data', function () {
it('should call the $.ajax method correctly', function (done) {
var spy = sinon.spy(oc.$, 'ajax');
var spy = sinon.stub(oc.$, 'ajax').callsFake(function (options) {
return options.success([
{ response: { renderMode: 'unrendered', data: 'hello' } }
]);
});

execute(
{
Expand Down

0 comments on commit 947d863

Please sign in to comment.