#pragma once
#include <memory>
#include <string>
#include <map>
class IAjaxAdapter
{
public:
virtual void ajax(std::wstring url, std::map<std::wstring, std::wstring> settings) = 0;
};
class App
{
public:
App(std::shared_ptr<IAjaxAdapter> ajaxManager);
};
#include "ExternalImpl.h"
#ifdef EMSCRIPTEN
#include <emscripten/bind.h>
using namespace emscripten;
#endif
App::App(std::shared_ptr<IAjaxAdapter> ajaxManager)
{
ajaxManager->ajax(L"https://kripken.github.io", {});
}
#ifdef EMSCRIPTEN
struct IAjaxAdapterWrapper : public wrapper<IAjaxAdapter> {
EMSCRIPTEN_WRAPPER(IAjaxAdapterWrapper);
void ajax(std::wstring url, std::map<std::wstring, std::wstring> settings) {
return call<void>("ajax", url, settings);
}
};
EMSCRIPTEN_BINDINGS(ExternalImpl) {
class_<IAjaxAdapter>("IAjaxAdapter")
.smart_ptr<std::shared_ptr<IAjaxAdapter>>("IAjaxAdapter")
.function("ajax", &IAjaxAdapter::ajax, pure_virtual())
.allow_subclass<IAjaxAdapterWrapper>("IAjaxAdapterWrapper")
;
class_<App>("App")
.smart_ptr_constructor("App", &std::make_shared<App, std::shared_ptr<IAjaxAdapter>>)
;
}
#endif
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<TITLE></TITLE>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.1.min.js" type="text/javascript"></script>
<script src="externalImpl.js" type="text/javascript"></script>
<script>
$(function () {
$("#writeToMe").text("Working");
var item = new Module.App(Module.IAjaxAdapter.implement({
ajax: function (url, settings) {
$("#writeToMe").text('Ajax function called');
}
}));
});
</script>
</HEAD>
<BODY>
<div id="writeToMe"></div>
</BODY>
</HTML>