hbc
hbc

Reputation: 43

Using JSON Api URL EmberJS

I normally get data from a service I created, a hard-coded JSON. But I need to take the JSON from an URL.

This is my twiddle :

https://ember-twiddle.com/b9cd8b1b3418d876f88235c4aa99e268?openFiles=templates.pic.hbs%2Ctemplates.components.image-list.hbs14

How can I add a URL as a source instead of calling it from the service 'pics'? I tried something but got errors and couldn't do anything. I'm very new at this.

I tried

model() {
   return $.getJSON('/my-url');
}

But I get this error :

Mirage: Your Ember app tried to GET 'my URL', but there was no route defined to handle this request. Define a route that matches this path in your mirage/config.js file. Did you forget to add your namespace?

I totally had no idea about the error because I don't use mirage, I created it yeah but didn't use in any part of the project.

Then I tried :

import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        this.get('my Json url', () => {
            return [];
        });
    }
});

Now chrome's devTool doesn't give any error but all I see is a blank-page. Is this all wrong or is it something about the .hbs files?

Any ideas?Thanks!

Upvotes: 0

Views: 99

Answers (2)

hbc
hbc

Reputation: 43

I figured what the problem was with some help :

Solution:

In my index.js, I should have used

{{image-list model=model.Data currentPos=currentPos }}

instead of

{{image-list model=model currentPos=currentPos }}

Also, I don't need any model/*.js because I get the model() from IndexRoute so I deleted those files. Thanks

Upvotes: 0

Daniel
Daniel

Reputation: 18680

Uninstall ember-cli-mirage and go back to:

model() {
   return $.getJSON('/my-url');
}

Upvotes: 1

Related Questions