Home Manual Reference Source Repository
public class | source

Item

Extends:

EventsModel → Item

Our basic todos Item model has content, order, and done attributes. New Item instances are created in App.js->createItem.

Member Summary

Public Members
public get

Default attributes for the item.

Method Summary

Public Methods
public

Ensure that each item created has content.

public

toggle()

Toggle the done state of this item.

Inherited Summary

From class Events
public

bind(): *

Delegates to on.

public

listenTo(obj: object, name: string, callback: function): Events

Tell an object to listen to a particular event on an other object.

public

listenToOnce(obj: object, name: string, callback: function): Events

Just like listenTo, but causes the bound callback to fire only once before being removed.

public

off(name: string, callback: function, context: object): Events

Remove a previously-bound callback function from an object.

public

on(name: string, callback: function, context: object): *

Bind a callback function to an object.

public

once(name: string, callback: function, context: object): *

Just like on, but causes the bound callback to fire only once before being removed.

public

stopListening(obj: object, name: string, callback: function): Events

Tell an object to stop listening to events.

public

Trigger callbacks for the given event, or space-delimited list of events.

public

unbind(): *

Delegates to off.

From class Model
public

The hash of attributes for this model.

public

A hash of attributes whose current and previous value differ.

public

Client side ID

public

The prefix is used to create the client id which is used to identify models locally.

public

A potentially associated collection.

public

id: *

Update the id.

public

The value returned during the last failed validation.

public

Retrieve a hash of only the model's attributes that have changed since the last set, or false if there are none. Optionally, an external attributes hash can be passed in, returning the attributes in that hash which differ from the model. This can be used to figure out which portions of a view should be updated, or what calls need to be made to sync the changes to the server.

public

clear(options: object): *

Removes all attributes from the model, including the id attribute.

public

clone(): *

Returns a new instance of the model with identical attributes.

public

Destroys the model on the server by delegating an HTTP DELETE request to Backbone.sync.

public

escape(attr: *): string

Similar to get, but returns the HTML-escaped version of a model's attribute. If you're interpolating data from the model into HTML, using escape to retrieve attributes will prevent XSS attacks.

public

fetch(options: object): *

Merges the model's state with attributes fetched from the server by delegating to Backbone.sync. Returns a jqXHR. Useful if the model has never been populated with data, or if you'd like to ensure that you have the latest server state.

public

get(attr: *): *

Get the current value of an attribute from the model.

public

has(attr: string): boolean

Returns true if the attribute is set to a non-null or non-undefined value.

public

hasChanged(attr: string): *

Has the model changed since its last set? If an attribute is passed, returns true if that specific attribute has changed.

public abstract

Initialize is an empty function by default.

public

Has this model been saved to the server yet? If the model does not yet have an id, it is considered to be new.

public

isValid(options: object): boolean

Run validate to check the model state.

public

Special-cased proxy to the _.matches function from Underscore.

public

parse(resp: object, options: object): object

parse is called whenever a model's data is returned by the server, in fetch, and save. The function is passed the raw response object, and should return the attributes hash to be set on the model. The default implementation is a no-op, simply passing through the JSON response. Override this if you need to work with a preexisting API, or better namespace your responses.

public

previous(attr: string): *

During a "change" event, this method can be used to get the previous value of a changed attribute.

public

Return a copy of the model's previous attributes. Useful for getting a diff between versions of a model, or getting back to a valid state after an error occurs.

public

save(key: key | object, val: *, options: object): *

Save a model to your database (or alternative persistence layer), by delegating to Backbone.sync.

public

set(key: object | string, val: * | object, options: object): *

Set a hash of attributes (one or many) on the model.

public

sync(): *

Uses Backbone.sync to persist the state of a model to the server.

public

Return a shallow copy of the model's attributes for JSON stringification. This can be used for persistence, serialization, or for augmentation before being sent to the server. The name of this method is a bit confusing, as it doesn't actually return a JSON string — but I'm afraid that it's the way that the JavaScript API for JSON.stringify works.

public

unset(attr: object | string, options: object): *

Remove an attribute by deleting it from the internal attributes hash.

public

url(): string

Returns the relative URL where the model's resource would be located on the server. If your models are located somewhere else, override this method with the correct logic. Generates URLs of the form: "[collection.url]/[id]" by default, but you may override by specifying an explicit urlRoot if the model's collection shouldn't be taken into account.

Delegates to Collection#url to generate the URL, so make sure that you have it defined, or a urlRoot property, if all models of this class share a common root URL. A model with an id of 101, stored in a Backbone.Collection with a url of "/documents/7/notes", would have this URL: "/documents/7/notes/101"

protected

_validate(attrs: object, options: object): boolean

Run validation against the next complete set of model attributes, returning true if all is well.

Public Members

public get defaults: object: * source

Default attributes for the item.

Return:

object

Public Methods

public initialize() source

Ensure that each item created has content.

Override:

Model#initialize

public toggle() source

Toggle the done state of this item.