new App( [opts])
Constructor class for the main framework app.
SUGAR.App contains the core instance of the app. All related modules
can be found within the SUGAR namespace.
An uninitialized instance will exist on page load but you will need to call App#init to initialize your instance.
By default, the app uses body element and div#content as root element
and content element respectively.
var app = SUGAR.App.init({
el: '#root',
contentEl: '#content'
});If you want to initialize an app without initializing its modules,
var app = SUGAR.App.init({el: '#root', silent: true});
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
opts |
Object |
<optional> |
Configuration options. See full list of options in #init. |
- Mixes In:
Returns:
Application instance.
- Type
- App
Members
-
$contentEl :jQuery
-
Content element selector.
The application controller loads layouts into the content element.
Type:
- jQuery
-
$rootEl :jQuery
-
Base element to use as the root of the app.
Type:
- jQuery
-
additionalComponents :Object
-
Additional components.
These components are created and rendered only once, when the application starts.
Application specific code is needed for managing the components after they have been put into DOM by the framework.
Type:
- Object
-
appId :string
-
Unique application ID.
Type:
- string
Methods
-
<static> augment(name, obj [, init])
-
Augments the application with a module.
Module should be an object with an optional
init(app)function. The init function is passed the current instance of the application when app's App#init method gets called. Use theinitfunction to perform custom initialization logic during app initialization.Parameters:
Name Type Argument Default Description namestring Name of the module.
objObject Module to augment the app with.
initboolean <optional>
false Flag indicating if the module should be initialized immediately.
-
<static> destroy()
-
Destroys the instance of the current app.
-
<static> init( [opts])
-
Initializes the app.
Parameters:
Name Type Argument Description optsObject <optional>
Initialization options.
Properties
Name Type Argument Default Description elstring <optional>
'body' The selector for the base element to use as the root of the app.
contentElstring <optional>
'#content' The selector for the content element.
silentboolean <optional>
false Flag to indicate if modules should be initialized during application init process.
defaultErrorHandlerfunction <optional>
Allows you to define a custom error handler. Defaults to Core.Error#handleHttpError.
Fires:
- app:init if `opts.event:silent` is not `false`.
- app:sync:error if the public metadata could not be synced.
Returns:
Application instance
- Type
- App
-
<static> isServerCompatible(data)
-
Checks if the server version and flavor are compatible.
Parameters:
Name Type Description dataObject Server information.
Returns:
trueif server is compatible and an error object if not.- Type
- boolean | Object
-
<static> loadCss( [callback])
-
Loads application CSS.
Will make an HTTP request and retrieve either a list of CSS files to load, or directly plain text css.
Parameters:
Name Type Argument Description callbackfunction <optional>
Function called once CSS is loaded.
-
<static> login(credentials [, info] [, callbacks])
-
Logs in to the app.
Parameters:
Name Type Argument Description credentialsObject User credentials.
Properties
Name Type Description usernameObject User name.
passwordObject User password.
infoObject <optional>
Extra data to be passed in login request such as client user agent, etc.
callbacksObject <optional>
Object containing the callbacks.
Properties
Name Type Argument Description successfunction <optional>
The success callback.
errorfunction <optional>
The error callback.
completefunction <optional>
The complete callback.
Fires:
- app:login:success on successful login.
-
<static> logout( [callbacks] [, clear] [, options])
-
Logs out of this app.
Parameters:
Name Type Argument Default Description callbacksObject <optional>
Object containing the callbacks.
Properties
Name Type Argument Description successfunction <optional>
The success callback.
errorfunction <optional>
The error callback.
completefunction <optional>
The complete callback.
clearboolean <optional>
false Flag indicating if user information must be deleted from cache.
optionsObject <optional>
{} jQuery/Zepto request options.
Fires:
- app:logout
Returns:
XHR request object.
- Type
- SUGAR.HttpRequest
-
<static> navigate( [context] [, model] [, action])
-
Navigates to a new route.
Parameters:
Name Type Argument Description contextCore/Context <optional>
Context object to extract the module from.
modelData/Bean <optional>
Model object to route with.
actionstring <optional>
Action name.
-
<static> setConfig( [config])
-
Updates the config object based on the new metadata.
Reloads modules that depend on the config.
Parameters:
Name Type Argument Description configObject <optional>
The config object. If not passed, we'll grab it using Core/MetadataManager#getConfig.
-
<static> start()
-
Starts the main execution phase of the application.
Fires:
- app:start
-
<static> sync( [options])
-
Syncs an app.
The events are not fired if the sync happens for public metadata.
Parameters:
Name Type Argument Description optionsObject <optional>
Options. See full list of options you can pass to Core.MetadataManager#sync.
Properties
Name Type Argument Default Description callbackfunction <optional>
Function to be invoked when the sync operation completes.
getPublicboolean <optional>
false Flag indicating if only public metadata should be synced.
Fires:
- app:sync when the synchronization process begins.
- app:sync:complete when the series of synchronization operations have finished.event:
- app:sync:error if synchronization fails.
-
<static> syncPublic( [options])
-
Syncs public metadata.
Parameters:
Name Type Argument Description optionsObject <optional>
Options. See full list of options you can pass to Core.MetadataManager#sync.
Properties
Name Type Argument Description callbackfunction <optional>
Function to be invoked when the sync operation completes.
-
before(name, callback [, context])
-
Adds a callback/hook to be fired before an action is taken. If that callback returns
false, the action should not be taken.The following example binds a callback function and passes the scope from the view component to use in that callback:
model.before('save', this.doSomethingBeforeSave, this);Multiple space-separated event names can be bound to a single callback:
view.before('save dispose', this.callback, this);This method also supports an event map syntax, as an alternative to positional arguments:
this.before({ render: this.doSomethingBeforeRender, dispose: this.doSomethingBeforeDispose, });Parameters:
Name Type Argument Description namestring | Object Event(s) to trigger before. Accepts multiple space-separated event names or an event map.
callbackfunction Function to be called.
contextObject <optional>
Value to be assigned to
thiswhen the callback is fired.- Mixes In:
Returns:
Instance of this class.
- Type
- Object
-
offBefore( [name] [, callback] [, context])
-
Removes a previously-bound callback function from a before event.
If no context is given, all of the versions of the callback with different contexts will be removed:
this.offBefore('render', this.onRenderBefore);If no callback is given, all callbacks for the before event will be removed:
this.offBefore('render');If no event is specified, all callbacks for all before events will be removed from the object:
this.offBefore();Parameters:
Name Type Argument Description namestring <optional>
Event(s) to remove the listeners for.
callbackfunction <optional>
Callback to remove specifically for a given event.
contextObject <optional>
Context to use when determining which callback to remove.
- Mixes In:
Returns:
Instance of this class.
- Type
- Object
-
triggerBefore(name)
-
Triggers the before callback for the given event
nameor list of events.The following example triggers the callback bound to the before
saveevent given:this.triggerBefore('save');Multiple events can be triggered as well:
this.triggerBefore('save render dispose');Custom arguments (e.g.
a,b,c) can be passed to the callback:this.triggerBefore('save', a, b, c);Parameters:
Name Type Description namestring The before event(s) to trigger.
- Mixes In:
Returns:
Returns
trueif the event should be triggered,falseotherwise.- Type
- boolean
Events
-
app:init
-
Fires when the app object is initialized. Modules bound to this event will initialize.
-
app:locale:change
-
Fires when client application's user changes the locale, thus indicating that the application should "re-render" itself.
-
app:login
-
- Fires when logging in.
-
app:login:success
-
Fires when login succeeds.
-
app:logout
-
Fires when the app logs out.
-
app:start
-
Fires when the application has finished loading its dependencies and should initialize everything.
-
app:sync
-
Fires when the app is beginning to sync data / metadata from the server.
-
app:sync:complete
-
Fires when the app has finished its syncing process and is ready to proceed.
-
app:sync:error
-
Fires when a sync process failed.
-
app:sync:public:error
-
Fires when a sync process failed during initialization of the app.
-
app:view:change
-
Fires when route changes a new view has been loaded.
-
lang:direction:change
-
Fires when the language display direction changes.
Possible language display directions are
RTLandLTR.