In this chapter, you'll learn how to:
Model
, Backbone's main class to work with dataModel
subclassesModel
attributes and trigger other code when these attributes changeModel
has borrowed from Underscore
Models in Backbone are the core of all data interactions, both within the client code itself and when communicating with a remote server. While one can simply use a plain JavaScript object instead of a Model
, Models offer three major advantages:
As discussed in the previous chapter, we can create our own subclass of Model using extend
:
var Cat = Backbone.Model.extend({ // The properties and methods of our "Cat" class would go here });
Once we've created that class, we can instantiate new Model
instances using JavaScript's new
keyword, and (optionally) we can pass in an initial set of attributes and options, as follows:
var garfield = new Cat({ name: 'Garfield', owner: 'John' }, { estimateWeight: true });
18.217.167.170