Custom Models in Ember.js

Like Adam says, Ember-Data is pre-alpha software. Some projects may not allow the time needed to debug, file issues and dig through an unfamiliar codebase. But Ember.js is much more stable and ready to use. So the problem becomes, without Ember-Data, how to put the M in MVC?

You could use plain-old Javascript objects. But Ember provides such a rich object model, coupled with live-updating templates, you'd be stupid not to take advantage of it. Fortunately, writing a very simple custom base model that plays nicely with its observer-based templates is not very difficult.

In order to write your own base model, you need to understand that Ember's live-updating is not magic. It's based on always working with the same object references at all times. Therefore we make heavy use of caching via Ember.Map.

We will also make use of a Promise to allow more flexible ajax requests. To ensure we process API responses asynchronously we'll resolve the promise inside an Ember run loop.

The example below shows a base model and an example usage. It populates the model using a real API and displays data via an implicit controller and view.

Published on by Joe Fiorini.