Framework

- About
- Guides
- Community
- API
- Code
- Download

# Kanso API Documentation

Index | View on single page

## utils

General utility functions used by Kanso. Some functions were moved here from other modules (such as core), to avoid a circular dependency bug in CouchDB.

This module also stores some useful properties such as 'isBrowser', which is true if the code is running in a browser environment, and 'initial_hit' which is set to true when a page is first requested from CouchDB (and set to false for subsequent requests).

### getBaseURL(req)

Returns the path to prefix to any URLs. When running behind a virtual host, there is nothing to prefix URLs with. When accessing the app directly, URLs need to be prefixed with /db/_design/appname/_rewrite.

The request object argument is only required when run server-side, but its a good idea to include it whenever you call getBaseURL.

#### Parameters

req | Object |

Returns: String

### getPropertyPath(obj, path)

Traverses an object and its sub-objects using an array of property names. Returns the value of the matched path, or undefined if the property does not exist.

If a string if used for the path, it is assumed to be a path with a single key (the given string).

    getPropertyPath({a: {b: 'foo'}}, ['a','b']) -> 'foo'
    getPropertyPath({a: {b: 'foo'}}, 'a') -> {b: 'foo'}

#### Parameters

obj | Object |
path | Array|String |

### setPropertyPath(obj, path, val)

Traverses an object and its sub-objects using an array of property names. Sets the value of the matched property.

If a string if used for the path, it is assumed to be a path with a single key (the given string).

    setPropertyPath({}, ['a','b'], 'foo') -> {a: {b: 'foo'}}
    setPropertyPath({}, 'a', 'foo') -> {a: 'foo'}

#### Parameters

obj | Object |
path | Array|String |

### escapeHTML(s)

Encodes required characters as HTML entities so a string can be included in a page.

#### Parameters

s | String |

Returns: String

### parseCSV(csvString)

Parse CSV strings into an array of rows, each row an array of values. Used by the array field's default CSV widget.

#### Parameters

csvString | String |

Returns: Array

### redirect(req, url)

Creates CouchDB response object for returning from a show, list or update function, which redirects to the given app url (automatically prepending the baseURL)

#### Parameters

req | Object |
url | String |

Returns: Object

### isSubPath(a, b)

Tests if path b is equal to or a sub-path of a.

#### Parameters

a | Array |
b | Array |

Returns: Boolean

### titleize(str)

Return a title-case version of the supplied string.

#### Parameters

str |  | The string to transform.

Returns: String

### bindContext(context, closure)

Returns a function that executes {closure} in the context of {context}. Use this function if you'd like to preserve the current context across callbacks, event handlers, and other cases where the value of {this} is set for you.

#### Parameters

context | Object | The context to use when executing closure.Usually, you will specify the current value of 'this'.
closure | Function | The function to to bind to {context}.