Thursday 14 May 2015

EMBER MODELS

Today I'm going to expand on a previous post, and have a closer look at how models in Ember work, specifically how they retrieve and display data. An Ember Model tells our application what it needs to know about the data it will be utilising. The attributes and behaviour of the data are defined here. A model in Ember has many similarities to a class in OO programming in that it can have many instances with each instance representing different values. 
# app/models/course.js
 MyDramaSchool.Course = DS.Model.extend({  
  title: DS.attr('string'),   
  cost: DS.attr('number'),   
  description: DS.attr('string'),   
  reviews: DS.hasMany('review'),   
  teacher: DS.belongsTo('teacher')  
 });  
The following data types are available;  

*string 
*number  
*boolean 
*date 

The model can define relationships between other models. Most commonly we will use the 'hasMany' and 'belongsTo'. These are similar to the relationships we might define in a rails model. http://guides.emberjs.com/v1.10.0/models/defining-models/  

Ember models also come with many useful built-in methods. Some of the most popular are;
model.save() #to save changes to the database  
model.rollback() #to reverse any changes made to the database  
model.destroyRecord() #to delete the record from the database  

The data that populates the model can come from two different sources; inside the application or from a connection to another data source. Most commonly you will use data sourced from an api but while still learning, EmberFixtures are a good fit. They are a way to put sample data into an application, 'almost' like hard-coding your data into an array. But for all intents and purposes they act like data we have retrieved from a server. They are ideal to work with when learning Ember due to their simplicity.
 MyDramaSchool.Course.FIXTURES = [{   
 id: 1, title: 'Speech',   
 cost: 99,   
 description: 'Ideal for all ages'   
 },{   
 id: 2,   
 cost: 79,   
 title: 'Drama',   
 description: 'Suitable for beginners'   
 }];   
Ember data has two Application Adapters and their job is to tell our application where it loads the data from and how. 
RESTAdapter: This adapter communicates over HTTP using JSON. You could use this when retrieving data from, perhaps, a rails application that has an api your Ember application consumes. 
FixtureAdapter: This adapter uses fixtures and is what we are using in the application. 
 MyDramaSchool.ApplicationAdapter = DS.FixtureAdapter.extend();  
Models typically use another library called Ember Data. Ember Data has a store, it's like a repository or cached storage for all your records. You can use it to set and retrieve records. Its job is to assist the model in retrieving the data as well as creating and saving new records. It also helps with caching the data for optimum performance. Read more on ember data at https://github.com/emberjs/data

To read more about how ember models work visit the emberjs website http://emberjs.com/