# Mojo.Model

## Namespace Detail

## Method Summary

- Mojo.Model.decorate(proto, clone)
- Mojo.Model.decrypt(key, data)
- Mojo.Model.encrypt(key, data)
- Mojo.Model.format(model, formatters, clone)

## Method Detail

### {Object} Mojo.Model.decorate(proto, clone)

The idea of a decorator is that the prototype references the original object, so properties can be transparently added to (or modified in) the decorator without affecting the original.

This is really handy for widgets that need to add properties to their model in preparation for rendering.

#### Parameters

- {Object} proto - the object to decorate. The new decorator object's prototype field will refer to this object.
- {Object} clone - if specified, will have its properties copied into the new decorator.

#### Returns

{Object} a new decorator object for the given 'original' object.

### {string} Mojo.Model.decrypt(key, data)

The string returned is a Base 64 decoded version of the Blowfish decrypted version of the source string. See this article for a description of Blowfish encryption. The strength of the encryption depends on the length of the key provided. For example, a 16-byte-long string is equivalent to a 128-bit key.

Example

>     var key = "sfhjasf7827387af9s7d8f";
>     var in_string = "This is a test string.";
>     var encrypted_string = Mojo.Model.encrypt( key, in_string );
>     var decrypted_string = Mojo.Model.decrypt( key, encrypted_string );

#### Parameters

- {string} key - The key used to encrypt the data. A discussion of proper key generation is beyond the scope of this document.
- {string} data - Base 64 encoded data to be decrypted.

#### Returns

{string} Base 64 decoded version of the Blowfish-decrypted version of the source string.

### {string} Mojo.Model.encrypt(key, data)

The string returned is a Base 64 encoded version of the Blowfish-encrypted version of the source string.

Example

>     var key = "sfhjasf7827387af9s7d8f";
>     var in_string = "This is a test string.";
>     var encrypted_string = Mojo.Model.encrypt( key, in_string );
>     var decrypted_string = Mojo.Model.decrypt( key, encrypted_string );

#### Parameters

- {string} key - Encryption key to be used.
- {string} data - Data to be encrypted.

#### Returns

{string} Base 64 encoded version of the Blowfish-encrypted version of the source string.

### Mojo.Model.format(model, formatters, clone)

Applies the relevant formatter functions in formatters, to the given model object. The formatter results are placed in a newly created decorator, so the original model is unmodified.

Formatter functions receive their property value as the first argument, and the whole model object as a second argument. They may return a string, which is used for the formatted version of the model property, or alternatively a hash of `formattedPropertyName -> formattedValue`, so one formatter function can create multiple formatted values from a single model property.

#### Parameters

- {Object} model - the object containing the properties to be formatted. It is left unmodified.

- {Object} formatters - a hash of property names to formatter functions. The keys in the 'formatters' hash are names of properties in the model object to which the formatter functions should be applied. Formatted values have the text "formatted" appended to their names, so the unformatted value is also available.

- {Object} clone - if specified, the clone object passed to 'decorate'. Its properties are copied to the model decorator object before applying the formatter functions.