콘텐츠 (Contents)
1. 메서드
init (SDK 초기화)
⚠️ SDK에서 제공하는 모든 기능은 init 메서드를 통해 초기화가 완료된 이후에 사용할 수 있습니다. 반드시 해당 함수의 실행이 완료된 이후 SDK 기능을 사용해주세요.
Vanilla JS example
<script defer>
document.addEventListener('DOMContentLoaded', function () {
// EliceContents SDK 초기화
var eliceContents = new EliceContents();
var rootEl = document.getElementById('root') || document.body;
eliceContents
.init()
.then(function () {
// SDK 초기화 완료
renderApp();
})
.catch(function () {
// SDK 초기화 실패
console.error('SDK 초기화 실패');
});
function renderApp() {
// App 컴포넌트 내용
var appContent = '<div>Hello, world!';
// root 요소에 App 컴포넌트 내용 추가
rootEl.innerHTML = appContent;
}
});
</script>
React example
// main.tsx
import { EliceContents } from '@eliceio/content-sdk';
const eliceContents = new EliceContents();
const rootEl = document.getElementById('root') ?? document.body;
const root = createRoot(rootEl);
eliceContents
.init()
.then(() => {
// SDK 초기화 완료
return root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
})
.catch(() => {
// SDK 초기화 실패
});
설치된 SDK에서 제공하는 init()
함수를 사용하여 SDK를 초기화합니다.
-
해당 함수는 기본적으로
window.location.search
를 통해 URL에 포함된 쿼리스트링을 파싱하여 엘리스 플랫폼과 연동하기 위한 정보인materialId
,extToken
을 추출합니다.// src/constants.ts
import { EliceContents } from '@eliceio/content-sdk';
export const eliceContents = new EliceContents();
eliceContents.init(); // 쿼리스트링에서 추출하여 초기화합니다. -
window.location.search에서 연동하기 위한 정보를 제공하기 어려울 경우,
init()
함수에search
인자를 통해 직접 정보를 제공할 수 있습니다.eliceContents.init({
search: queryString.stringify({
materialId: 100,
extToken: '...',
}),
}); -
baseUrl은 기본적으로 엘리스 플랫폼 production 환경에 설정되어 있습니다. 테스트 환경에서 사용하려면 baseUrl을 인자로 설정해주세요.
eliceContents.init({
baseUrl: 'https://dev-v4-external-contents-api.dev.elicer.io',
});
sendScore (엘리스 플랫폼으로 점수 전달)
콘텐츠를 사용자가 모두 완료하였을 때, 점수를 엘리스 플랫폼으로 전달하도록 구성합니다.
-
콘텐츠가 왼료되는 시점에
@eliceio/content-sdk
에서 제공하는sendScore()
함수를 호출합니다. 함수에는score
라는 인자 값이 필요로 하며, 일반적으로 콘텐츠를 완료하였을 때에는100
을 보내도록 합니다.import { eliceContents } from 'src/constants';
eliceContents.sendScore({ score: 100 }); // 100점을 엘리스 플랫폼으로 전달