Remote Resources

A short tutorial that shows how to work with server-side remote resources.

Alt-seven has a remote module that facilitates working with remote resources on your server. First, you need to specify where your remote methods live, in the application config. Below is a section of the application confif in app.js in the LacesIDE application. app.remote is specified as a object containing a key for each remote module.

var app = {
	main: main,
	auth: auth,
	remote: {
	apps: apps,
	applibraries: applibraries,
	libraries: libraries,
	profile: profile,
	user: user
	},
	ui: ui,
	utils: utils
};

The object is then used in the options for the application init, like so, also from LacesIDE:

	remote: {
			modules: app.remote,
			loginURL: '/api/auth/login',
			logoutURL: '/api/auth/logout',
			refreshURL: '/api/auth/refresh',
			useTokens: true, // defaults to true for the auth system
		},

See the Configuration tutorial for details on the options here. Note that useTokens defaults to true, as it enables session management through the built-in alt-seven authentication system. See the Authentication tutorial for more information there.

Remote methods are typically structured as modules for related methods. Again, drawing from LacesIDE, below you can see the profile module:

import { a7 } from '/lib/altseven/dist/a7.js';

export { update };

var update = function (obj) {

	var request;

	var params = {
		method: 'PUT',
		headers: {
			'Accept': 'application/json, application/xml, text/play, text/html, *.*',
			'Content-Type': 'application/json; charset=utf-8'
		},
		body: JSON.stringify({
			profilePic: obj.profilePic
		})
	};

	return a7.remote.fetch("/api/user/" + obj.userID + "/profile", params, true);
};

The remote methods in LacesIDE generally work in conjunction with the events system. In this case, the remote methods themsleves tend to be very small, containing only the information needed to make the remote call, and a fetch call to the remote resource that is returned to the method caller. As you can see here, using the built-in authentication system simplifies the remote calls by hiding everything associated with authentication and session management. It is all handled seamelessly in the background. If you want to learn more about the details of the authentication system, check the Authentication tutoral.

When remote resources are used in conjuncion with the events system, usually the heavy lifting is done in the calling event.

	a7.events.subscribe("profile.update", function (obj) {
			a7.remote.invoke("profile.update", obj)
				.then(function (response) {
					// get json response and pass to handler to resolve
					return response.json();
				})
				.then(function (json) {
					if (json.success) {
						utils.showNotice("Profile saved.", "#pTab1Notice");
						a7.events.publish("profile.refreshProfile");
					}else{
						utils.showNotice("Profile not saved.", "#pTab1Notice");
					}
				});
		});

In this example, the profile.update event calls the remote resource, then handles the response. While it is not necessary to use events like this to call remote resources, this is the standard program flow in alt-seven.