mercredi 24 octobre 2018

How to store filtered data from JSON and display in table (EmberJS)

I am trying to display a table of records on page load in EmberJS. The data comes from an ajax call, which gives a very large JSON response. Here is an overview of the process (how it should work): Page Loads -> Ajax call ( in controller) -> Response -> Store Data somewhere (filtered) -> Display in Table

So far I have been successful in getting a JSON response, but the data is huge and contains multiple fields and nested fields. I only need to store certain data from the response such as name, email etc.

Here is the code for my controller which is run when the page loads:

import Controller from '@ember/controller';
import Ember from 'ember';

export default Controller.extend({
ajax: Ember.inject.service(),
init:function(){
  const token = sessionStorage.getItem('token')
  this.get('ajax').request('http://127.0.0.1/SugarPro-Full-8.0.0/rest/v10/Leads',{
    method: 'POST',
    headers:{
    'Content-Type': 'application/json',
    'oauth-token': `${token}`
    },
    success: function(response){
      console.log(response);
      sessionStorage.setItem("data", response);
    },
    error: function(){
      window.alert("Please Login!");
      window.location.assign("/login-page");

    },
    dataType: 'json'
  });
}
});

NOTE: I'm storing the response in sessionStorage because I do not know where to go from here, so that may be ignored if required.

PS: I did see a few examples that suggested to put everything in the router, but that was when they assumed the response was a subset of the model, whereas in my case it's probably the other way around.

Many Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire