How can I load any json from my page?


#1

Hi,

I try to load a json file with an ember adapter + model but my json call failes with an error.

This is my code within my findRecord method in my application adapter: findRecord: function findRecord(store, type, id, snapshot) { return new Ember.RSVP.Promise(function (resolve, reject) {

    Ember.$.getJSON('/ui-page/index.json').then(function (data) {
      resolve(data);
    }, function (jqXHR) {
      reject(jqXHR);
    });    

My json call is ever rejected … I need a little simple example that I can see what I do wrong.

I hope you can help me. Thanks,

Greetings, Mario


#2

You should be using ember-cli-mirage and configure the same to return your json.

You may also refer https://emberigniter.com/mock-json-api-backend-with-mirage as tutorial


#3

You can also place the JSON files in your public folder


#4

My json is placed in public/ui-page/index.json

Currently this is my simple test call: Ember.$.getJSON(’/ui-page/index.json’).then(function (data) { resolve(data); }, function (jqXHR) { reject(jqXHR); });

The response ends ever in the catch ( rejected ) case. Why?

Mario


#5

Your approach is deviating from standard guidelines, which may be due to insufficient understanding of Ember Routing mechanism.

Any contextual path in ember application is considered as route, and there is NO route definition for the mentioned path. Please refer Core Concepts and Routing in guides for better understanding.

As already mentioned, you should be using ember-cli-mirage with fixtures for your scenario.

if you still want to load index.json as static asset, you might want to place it under public folder.

HTH.


#6

Hi… yes I know. This was only a test. Now I use an standard RSTAdapter with this call for example:

this.get(‘store’).findRecord(‘ui-page’, ‘index’);

My question now is: How must be the structure of my json file when my model looks so:

export default DS.Model.extend({ type: DS.attr(‘string’), children: DS.attr() });

Mario