load method
Implementation
Future load(String url, {required bool refresh, String? dependency}) async {
try {
// parse the url
Uri? uri = URI.parse(url);
if (uri == null) throw ('Error');
// fetch the template
var template = await TemplateManager()
.fetch(url: url, parameters: uri.queryParameters, refresh: refresh);
// get the xml
var xml = template.document!.rootElement;
// template requires rights?
int? requiredRights = toInt(Xml.attribute(node: xml, tag: 'rights'));
if (requiredRights != null) {
int myrights = System.currentApp?.user.rights ?? 0;
bool connected = System.currentApp?.user.connected ?? false;
// logged on?
if (!connected) {
// fetch logon template
template = await TemplateManager().fetch(
url: System.currentApp?.loginPage ?? "login.xml", refresh: refresh);
xml = template.document!.rootElement;
}
// authorized?
else if (myrights < requiredRights) {
// fetch unauthorized template
template = await TemplateManager().fetchErrorTemplate(FetchResult(
code: HttpStatus.unauthorized,
message: Phrases().unauthorizedAccess,
detail:
"This page requires rights at or above level $requiredRights. You only have rights level $myrights for page $url"));
xml = template.document!.rootElement;
}
}
// register late scope
var alias = Xml.attribute(node: xml, tag: "id");
if (scope != null && alias != null) {
System.currentApp?.scopeManager.add(scope!, alias: alias);
}
// set template name
templateName = uri.replace(queryParameters: null).toString();
if (dependency != null) this.dependency = dependency;
// deserialize from xml
deserialize(xml);
// If the model contains any databrokers we fire them before building so we can bind to the data
// This normally happens in the view initState(), however, since the view builds before the
// template has been loaded, initState() has already run and we need to do it here.
initialize();
} catch (e) {
String msg = "Error building model from template $url";
Log().error(msg);
return Future.error(msg);
}
}