Models in-depth

In notebook:
FrontEndMasters BackboneJS
Created at:
2016-06-09
Updated:
2016-06-09
Tags:
libraries JavaScript Fundamentals
Will talk about plugging in models to third-party services later

Model

Model: Represents a single resource returned from a server

Used for storing data in-memory.

Different than just caching, it has: conversions, validations, computed properties, access control, event system

has getter/setter methods to access data (like in ES6)

Adds an event system - to broadcast changes. It’s very simple to implement. Works everywhere (old browsers).

The internal attributes of a model are exposed publicly. You can see it in the browser console. 

Models are very flexible

Don’t necessary need an API endpoint on the server.

How do you make models flexible?

​.parse()
​.toJSON()
​.sync()

parse is like JSON.parse and toJSON is like stringify.

You can easily override all of these. You can store any type of data in a model. But if you have nested data, you lose some of the events.