Open Event Front-end uses ember data which is a data persistence library for Ember.js. It uses promises to manage loading and saving records using JSON API format. Ember data uses models to which represent the API response to handle all the requests to be made to the server. Lets see how do we retrieve events & create new events using the Orga Server.
Installing ember data
Creating a model
We need a model in ember data which is a class containing all the attributes in the API response. We can create a model using ember cli command:
Populating the model
We must add all the attributes from the API response in the newly generated event model. Ember data automatically takes care of the ajax calls with high level of abstraction using promises.
We add all the attributes in the Orga server API response to the event model class.
Once we create the model containing all the attributes we are all set to make API requests in our application. We will now see how to make GET & POST requests in Open Event Front-end project.
Ember data lets us retrieve records of a single type using store. The records to be fetched must have respective model in the application. We can make a GET request using
This will return a model which contains all the events from the Orga Server, it also contains the events from past as well as the events which are unpublished.
To get all the published events we use the query method which lets us add query parameters to the request. We will filter the events on the basis of the state and the end time of the event.
Ember data lets us create & update records using the createRecord method of the store. We can create an event using
event = this.store.createRecord(‘event’)
This will create a record locally in the application to send a POST request we use the save() method which send the request to the server.
Thank you for reading the blog, you can check the source code for the example here.
We will discuss in detail how to implement API in Open Event Front-end in the next post.