mardi 7 juin 2016

using Ember-data with JSONApi and opening page in a new tab

I have a page that has pictures (index.js) and when you click a picture, a detail page with bigger version of the picture and its content (pic.js) opens. When I was using hard-coded data, I created a service and put the data in it. By this way, the model hook wasn't skipped when I click a picture. I did it because my links are dynamic helper and I saw that model hook gets skipped when you have it. But now, I need to use JSON api to get the data from an URL, when I do it in the index.js there is no problem with displaying it but when I try to open any link in new tab or paste a link in URL bar, model hook doesn't work in pic.js.

//routes/index.js

import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        return Ember.$.getJSON('My jsonApi Url');
    }
});

I read that I need to use ember-data in order to fix it. I created a model "news-list" and put attributes in it. Also I created an adapter and take the code which I call API from index.js and put there.

//adapters/application.js

import JSONAPIAdapter from 'ember-data/adapters/json-api';
import Ember from 'ember';

export default JSONAPIAdapter.extend({

    model(params){

        return Ember.$.getJSON('My jsonApi Url',params.NewsUrl);

    }
});

//templates/pic.hbs

<p class= "back">Anasayfa</p>
<p class="detail"><img src="" width="600" ></p>
<p class="content"><br/><br/></p><br/><br/>
<p class= "back">Anasayfa</p>



//templates/index.hbs




//templates/components/image-list.hbs


  <div>    
     <p class="info"></p><br/>
     <img src= width="300">
  </div> 


I tried to use return this.store.findAll('news-list'); in the pic.js but then all I see was a blank page when I click a picture. I guess there is something I'm missing. I can't use ember-data properly. How can I fix it?




Aucun commentaire:

Enregistrer un commentaire