Evented Models

When an object has a PubSub interface, we call it an evented object. A special case is when an object used to store data (a model) publishes events whenever its contents are modified. Models are the M in Model-View-Controller (MVC), which has become one of the hottest topics in JavaScript programming in the last few years. The core concept is that MVC applications are data-centric so that model events impact the DOM (aka the View) and the server (via the Controller).

Let’s look at the hugely popular Backbone.js framework.[29] You create a new model like so:

​ 
style = ​new​ Backbone.Model(
​ 
{font: ​'Georgia'​}
​ 
);

model just represents the simple object that was passed in.

​ 
style.toJSON() ​// {"font": ...

Get Async JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.