2

Hi I want to create a pagination for my application for that i am using spring-boot Pagination and ember-cli-pagination. Before using spring-boot pagination, rest controller returns data in the following format,

[{
"id" : 56789,
"name" : "sample book",
"status": "available",
},{
"id" : 56789,
"name" : "sample book",
"status": "available",
}]

after using spring-pagination my rest controller returns data in the format,

{
"content": 
[{
"id" : 56789,
"name" : "sample book",
"status": "available",
},{
"id" : 56789,
"name" : "sample book",
"status": "available",
}],
"last": true,
"totalElements": 5,
"totalPages": 2,
"size": 3,
"number": 1,
"sort": null,
"first": false,
"numberOfElements": 2
}

Now the structure of the data completely changes so i need to modify my ember model so that ember data will properly serialize my data.

Is there any other way to apply spring-boot-pagination to ember data with minimal changes?Any standard or guided way to do that?

Do i really need to change my ember model?

Ember Model :

export default DS.Model.extend({
   id: DS.attr(),
   name: DS.attr(),
   status: DS.attr()
});

Route.js

import Ember from 'ember';
import RouteMixin from 'ember-cli-pagination/remote/route-mixin';

export default Ember.Route.extend(RouteMixin, {

model: function(params) {
params.paramMapping = {
  perPage: "size"};
return this.findPaged('book',params);
},

resetController: function (controller) {
var queryParams = controller.get('queryParams');
queryParams.forEach(function (param) {
  controller.set(param, null);
});
},

});

Controller.js

import Ember from 'ember';
import pagedArray from 'ember-cli-pagination/computed/paged-array';

export default Ember.Controller.extend({
queryParams: ["page", "perPage",'uid'],
pagedContent: pagedArray('content', {pageBinding: "page", perPageBinding: "perPage"}),
totalPagesBinding: "pagedContent.totalPages",
page: 0,
perPage: 2,

//some actions
});

In the browser network tab i can able to see the response but in browser console i am getting the following message,

Error while processing route: book.index data is undefined _pushInternalModel

In Chrome getting following exception:

ember.debug.js:30610 Error while processing route: books.index Cannot read property 'type' of undefined TypeError: Cannot read property 'type' of undefined

and my teomplate is not loading.

index.hbs

<div class="row">
<div class="col-md-2">
    <h3>
        Books
    </h3>
</div>
</div>
<br/>
{{#each pagedContent}}
<h2>entered paged content</h2>
{{/each}}

{{page-numbers content=pagedContent}}

Any help should be appreciable.

VelNaga
  • 3,593
  • 6
  • 48
  • 82
  • You don't need to change your model, you'll need to create a `custom serializer` (for the model if it's the only one with that issue, or for the whole app it it's app-wide). You can see an example in the [ember docs](https://guides.emberjs.com/v2.8.0/models/customizing-serializers/) – Pedro Rio Oct 05 '16 at 10:29
  • @PedroRio Thanks a lot for your comments. I have edited my question.I read the documentation it seems "this.findPaged" will do all the work for us.Please correct me if i am wrong. – VelNaga Oct 05 '16 at 10:31
  • Sorry, I don't know the `ember-cli-pagination` addon. You should have `{{#each pagedContent as |content|}} {{content.SomePropery}} {{/each}}` though – Pedro Rio Oct 05 '16 at 10:44
  • @PedroRio still the problem persists.I think this.findPaged returns promise do you know how to resolve this promise?Or do you know how to check whether it's returns a promise or synchronous one? – VelNaga Oct 05 '16 at 10:52
  • The `model` hook on the Route is promise aware, so you can return a promise and it will work fine. I recommend you try to use [Ember Twiddle](http://ember-twiddle.com) to create a reproducing problem if possible, so that you can share it here and others can try to see if they can help – Pedro Rio Oct 05 '16 at 10:56
  • @PedroRio Could you please assist me to serialize a json i posted in the question. – VelNaga Oct 05 '16 at 14:00
  • @PedroRio Hoping serializing the data would resolve the error. – VelNaga Oct 05 '16 at 14:01

0 Answers0