Storm gives structure to web applications by providing models with getters, setters and events, centralized data contexts, a fast pubsub and lightweight promises.

Dependencies

Storm requires Underscore.js (1.6.0 or greater) and Signal. For ajax support include jQuery (or another library with a jQuery compatible API) and attach to Storm.ajax.

Downloads

Development 81.1 KB with comments Production 7.98 KB minified and gzipped Development (with Signal) 89.2 KB with comments Production (with Signal) 8.86 KB minified and gzipped

.Extend(constructor, extension)

Prototypical class extension

See:

.uniqId(prefix)

Returns: Id

Generates an ID number that is unique within the context of the current Storm instance. The internal counter does not persist between page loads.

  • prefix

    Type: String
    • optional

    Defines an optional scope (i.e., namespace) for the identifiers.

.when(promises)

Returns: Storm.Promise

When to go with Promise. Used by calling Storm.when() and passing promises to listen to. Storm.when can be chained with multiple calls e.g. Storm.when(p1, p2, p3).then(func).then(func).done(func).always(func);

mixin

Allows you to extend Storm with your own methods, classes and modules. Pass a hash of {name: function} definitions to have your functions added.

noConflict

Return the Storm global to its previous assignment and return Storm back to caller.

request

Ajax tracking mechanism. Operates via events passing the AjaxCalls that trigger the events. Possible events are: send, done, fail, abort, always

.getQueue()

Returns: Object

Get the requests in-progress.

.getTotal()

Returns: Number

Get the total number of requests in-progress.

.toString()

Returns: String

Debug string

template

Centralized template registration for holding, compiling and rendering client-side templates The template engine only has one requirement, a compile function that returns a render function. The render function will be called with the data as the first parameter. Common libraries that use this paradigm are: Mustache, Handlebars, Underscore etc...

.setEngine(engine)

Sets the client-side templating engine for Storm.template to use.

.toJSON(key)

Returns: String | Object

Return the registered template strings

  • key

    Type: String
    • optional

    a specific template

.toString()

Returns: String

Debug string

tick

A hook into a polyfilled requestAnimationFrame. Keeps a single raf that can be hooked into and prevent multiple implementations of raf.

.hook(func)

Returns: Id | Array.<Id>

Add a function to raf.

  • func

    Type: Function | Array.<function()>

    A function or an array of functions to hook.

.isRunning()

Returns: Boolean

Check if animate is running.

.start()

Returns: Storm.tick

Start raf calling hooked functions.

.stop()

Returns: Storm.tick

Stop raf from calling hooked functions.

.unhook(id)

Returns: Storm.tick

Remove a function from raf.

  • id

    Type: Id

    Hook function ID to remove.

AjaxCall

new Storm.AjaxCall(callObj, opts, callTemplate)

A wrapper for an ajax call configuration (referred to as a "call"). This object can ajax, abort and be passed around the application.

Members

CLASSIFICATION

Type:

defaults

Type:

Defaults.

.addClassification(type)

Add a classification type to the AjaxCall as a global option

.abort()

Returns: Storm.AjaxCall

Aborts the current request

.complete()

Fired when an xhr request completes. Feel free to override.

.data(data)

Returns: Storm.AjaxCall | *

Get or set the data on the call. Passing a parameter will set the data where-as no parameters will return the data on the call

  • data

    Type: *
    • optional

.error()

Fired when an xhr request completes. Feel free to override.

.get(key)

Returns: *

Gets a property on the call configuration

  • key

    Type: String

    the key to get from the configuration

.getConfiguration()

Returns: Object

Returns the call object

.getId()

Returns: Id

Get the private id of the AjaxCall

.isClassification(classification)

Returns: Boolean

Determine if the classification passed is the same as this call's classification

.isType(type)

Returns: Boolean

Determine if the type passed is the same as this call's configuration

  • type

    Type: String

    GET, POST, PUT, DELETE

.send(promise)

Returns: Storm.Promise

Uses Storm.ajax to ajax the stored call object

.set(key, value)

Returns: Storm.AjaxCall

Sets a property on the call configuration

  • key

    Type: String

    the key to replace in the configuration

  • value

    Type: *

    the value to apply

.success(data)

Fired when an xhr request is successful. Feel free to override.

.toString()

Returns: String

Debug string

Cache

new Storm.Cache()

An in-memory key-value store

.flush()

Returns: Storm.Cache

Resets the cache, emptying it and clearing any expirations

.get(key)

Returns: * | Array.<*>

Gets an item from cache or an array of values

.remove(keys)

Returns: Storm.Cache

Remove an item from cache or multiple items if an array is passed

.store(key, data, opts)

Returns: Storm.Cache

Stores data in the cache

.toJSON(key)

Returns: Object | Array

Gets the value (or entire cache) to serialize to JSON

.toString()

Returns: String

Debug string

Collection

new Storm.Collection(data)

A collection of Models

Members

.add(models, opts, data)

Add models to the collection, creating new models if the model is not an instance of Storm.Model, sorting the models and firing events

  • models

    Type: Array.<Storm.Model>

  • opts

    Type: Object
    • optional

  • data

    Type: Object

    additional data to pass to the new models

.at(idx)

Returns: Storm.Model

Retrieve a model at the provided index

.clear()

Returns: Storm.Collection

Clear all of the models from the collection

.clone()

Returns: Storm.Model

Returns a clone of the collection

.findById(id)

Returns: Storm.Model

Find a model by id

  • id

    Type: Id

    model _id

.findWhere(values)

Returns: Storm.Model

Proxy for .where(values, true)

.getBy(key, value)

Returns: Storm.Model

Get a model by key-value

.getById(id)

Returns: Storm.Model

Gets a model by its id

.getId()

Returns: Id

Get the private id of the collection

.getModels()

Returns: Array.<Storm.Model>

Get all models

.indexOf(model)

Returns: Number

Get the index of a model

.length()

Returns: Number

Return the length of the models, same as getModels().length

.newModel(model, opts)

Returns: Storm.Model

Create a new model

.overwrite(models)

Returns: Array.<Storm.Model>

Overwrites the private models array with a new array of models

  • models

    Type: Array.<Storm.Model>

.pluck(key)

Returns: Array.<*>

Get all model values of the provided key

.pop(opts)

Returns: Storm.Model

As you would expect

  • opts

    Type: Object

    will be passed to remove

.push(model, opts)

Returns: Storm.Model

As you would expect

.remove(models, opts)

Returns: Storm.Collection

Remove a model from the collection

.reset(opts)

Returns: Storm.Collection

Drops all models from the collection

.retrieve()

Returns: Array.<Object>

Similar to Storm.Model.retrieve, returns all model data in the collection

.set()

Proxy for add

.shift(opts)

Returns: Storm.Model

As you would expect

  • opts

    Type: Object

    will be passed to remove

.slice(begin, end)

Returns: Array.<Storm.Model>

As you would expect

.sort(opts)

Returns: Storm.Collection

Force the collection to re-sort itself. You don't need to call this under normal circumstances, as the collection will maintain sort order as items are added.

.toJSON()

Returns: Array.<Object>

Return the data to serialize to JSON

.toString()

Returns: String

Debug string

.unshift(model, opts)

Returns: Storm.Model

As you would expect

.where(values, first)

Returns: Storm.Model | Array.<Storm.Model>

Get models matching the key-values passed

  • values

    Type: Object

    Hash containing key-value pairs

  • first

    Type: Boolean
    • optional

    return first found

Comparator

new Storm.Comparator(key, type)

Used by the Collection to sort Models. Having a separate object used by the model normalizes sorting and allows optimization by caching values

Members

HOIST

Type:

Default string to use if no value is present to compare against.

SORT

Type:

Sort types

Default Value:
alphabetical

.addSort(type)

Add a sort type to the Comparator as a global option

.alphabetical(model)

Returns: String

Default alphabetical sort. This method gets the value from the model and ensures a string return value

.bind(model)

Bind to the key on the model that the comparator watches. If the key changes, invalidate the sort value so that it's recalculated

.date(model)

Returns: Date

Default date sort. This method gets the value from the model and ensures a date return value

.getSortValue(model)

Returns: *

Get the value to sort by

  • model

    Type: Storm.model

.invalidateSortValue(model)

Returns: Storm.Comparator

Invalidates the sort value of a model by deleting it from the store

.numeric(model)

Returns: Number

Default numeric sort. This method gets the value from the model and ensures a number return value

.toString()

Returns: String

Debug string

DataContext

new Storm.DataContext()

Used to construct ajax calls to communicate with the server. Is a central location for configuration data to get and send data about models and collections

Members

settings

Type:

Default settings for the DataContext, where we are is usually useful

AjaxCall

Type:

AjaxCall, the constructor of the AjaxCall to use when configuring a new call object

callTemplate

Type:

Overrides AjaxCall's defaults

defaults

Type:

options

Type:

The merged defaults and passed options

.addSettings(settings)

Returns: Object

Add settings to the global DataContext.settings object. Basically a protected _.extend

.getSetting(setting)

Returns: *

Get a setting from the global DataContext.settings

.createCall(call, extensionData)

Returns: Storm.AjaxCall

Creates and returns a new AjaxCall

  • call

    Type: Object

    see AjaxCall.defaults

  • extensionData

    Type: Object

    Addition configurations for the url

.extendOptions(opts)

Returns: Storm.DataContext

Add more options to this data context

.getSetting(setting)

Returns: *

Get a setting from the global DataContext.settings

.removeOption(key)

Returns: Storm.DataContext

Remove an option from this data context

.toString()

Returns: String

Debug string

Events

Model

new Storm.Model(data, opts)

The base data object for the application. Stores and protects a piece of data and gives an interface to manipulate it. Works in conjunction with a Collection to organize data into sets.

Members

defaults

Type:

The defaults to be merged into the data object when constructed

supportComplexDataTypes

Type:

If not supporting complex data types (default), the model creates a reference-free version of the data to keep the data from being contaminated. If supporting complex data types, non-primitive values will be maintained in the model data, but exposes the possibility of contaminating the data object by outside sources

Default Value:
false

.add(prop, data, opts)

Returns: Storm.Model

Adds properties/values to the model data. Only works to add additional data to the model data, it will not modify any pre-existing data. An object can be passed to set multiple key-values or a string and value as separate parameters

.clone()

Returns: Storm.Model

Returns a clone of the model

.compareTo(comparisonModel)

Returns: Number

Compares this model to another. Used by Collection for sorting. Checks for Storm.Comparator to use natively but can be overwritten

.get(prop)

Returns: * | Array.<*>

Get a value from the Model, passing it through the getter method if one exists. An array can be passed to get multiple values or a string to get a single value

.getId()

Returns: Number

Get the private id of the Model

.getter(prop, func)

Returns: Storm.Model

Adds a getter for the property

.has(prop)

Returns: Boolean

Checks if the model data has the provided property

.hasChanged(prop)

Returns: Boolean

Checks if the model data has changed from the original data. If a prop is passed, then it will check if that property's value has changed and not the entire model data

.previous(prop)

Returns: *

Returns the previous value for the property

.remove(prop, opts)

Returns: Storm.Model

Removes properties from the model data. Only works to remove items that pre-exist in the data. No remove event will be fired if the property has an undefined value. An array can be passed to remove multiple properties or a string as a single parameter

.restore(opts)

Returns: Storm.Model

Restores the data of the model back to the original value

.retrieve()

Returns: Object

Retrieve the model data

.set(prop, data, opts)

Returns: Storm.Model

Sets the value of a property in the Model. Values can be set in key-value pairs in an object, or as string/value as separate parameters

.setter(prop, func)

Returns: Storm.Model

Adds a setter for the property

.toJSON()

Returns: Object

Returns the data for serialization

.toString()

Returns: String

Debug string

Module

Promise

Storage

new Storm.Storage(type, opts)

Based off of Remy's polyfill. Adapted to use the same Storage object for both local and session storage.

Members

TYPE

Type:

The storage type: local or session

.clear()

Returns: Storm.Storage

Clear all data from storage

.getItem(key)

Returns: *

Retrieve item from data

.key(idx)

Returns: String

Get a key at the specified index

.remove(key)

Returns: Storm.Storage

Remove an item from storage by key

.removeItem(key)

Returns: *

Remove an item from storage by key

.setItem(key, value)

Adds to data

.toJSON(key)

Returns: *

Return storage values for JSON serialization

  • key

    Type: String
    • optional

    return a specific value

.toString()

Returns: String

Debug string

View

new Storm.View(opts)

A view at its most basic. Sets up a couple defaults for cloning and commonly used methods

.clone()

Returns: Storm.View

Returns a clone of the view

.getElem()

Returns: Element

Returns the cached elem or caches and returns an elem using render

.getId()

Returns: Number

Get the private id of the view

.render()

Returns: String

Generates the HTML markup for the view. Must be overridden by subclasses.

.toString()

Returns: String

Debug string

Fork me on GitHub