1

I do an example like this,but still can't get pagination

this is my store.js.coffee

Eme.serializer = DS.RESTSerializer.create()

Eme.serializer.configure
  meta: 'meta'
  pagination: 'pagination'

Eme.CustomAdapter = DS.RESTAdapter.extend
  serializer: Eme.serializer
  namespace: "api/v1"

Eme.Store = DS.Store.extend
  revision: 13
  adapter: 'Eme.CustomAdapter'

this is my controller

Eme.PluginsController = Em.ArrayController.extend
  content: []
  pagination: (-> 
    if this.get('model.isLoaded')
      console.log @get('model.type')
      console.log @get('store').typeMapFor(modelType).metadata

      modelType = @get('model.type')
      @get('store').typeMapFor(modelType).metadata.pagination
  ).property('model.isLoaded')

this is response

{
  "meta":{
    "pagination":{
      "total_count":16,
      "total_pages":2,
      "current_page":1
    }
  },
  "plugins":[{
    "id":"1",
    "name":"zhangsan",
  }]
}

this is my log:

Eme.Plugin

Object {}

Community
  • 1
  • 1
JUO
  • 626
  • 1
  • 7
  • 19

1 Answers1

2

In the example you pasted, the modelType variable is output to console before it has been defined. That could be why you are not seeing the pagination data as expected.

I've created a jsbin with a slightly modified version of your code and it appears to output pagination data correctly. See: http://jsbin.com/anIKAfO/2/edit

App = Ember.Application.create({});

App.IndexRoute = Ember.Route.extend({
  model: function(){
    return App.Plugin.find();
  }
});

App.IndexController = Ember.ArrayController.extend({
  pagination: function() {
    if (this.get('model.isLoaded')) {
      var store = this.get('store');
      modelType = this.get('model.type');
      console.log('modeltype: ', this.get('model.type'));
      var metadata = store.typeMapFor(modelType).metadata;
      console.log('metadata: ', metadata);
      return metadata.pagination;
    }
  }.property('model.isLoaded')
}); 

App.Store = DS.Store.extend({
  adapter: 'App.Adapter'
});

App.Plugin = DS.Model.extend({
  name: DS.attr('string')
});

App.serializer = DS.RESTSerializer.create();

App.serializer.configure({
  meta: 'meta',
  pagination: 'pagination'
});

App.Adapter = DS.RESTAdapter.extend({
  serializer: App.serializer,
  ajax: function(url, type, hash) {
    console.log('App.Adapter.ajax:', url, type, hash);
    json = App.RESTDATA[url];
    if (json) {
      console.log('App.Adapter.ajax: Found RESTDATA: ', json);
      return new Ember.RSVP.Promise(function(resolve, reject) {
        Ember.run(null, resolve, json);
      });
    } else {
      console.log('App.Adapter.ajax: No RESTDATA for url, calling API', url);
      return this._super(url, type, hash);
    }
  }
});

App.RESTDATA = {
'/plugins':
  {
    "meta":{
    "pagination":{
      "total_count":16,
      "total_pages":2,
      "current_page":1
    }
    },
    "plugins":[{
    "id":"1",
    "name":"zhangsan"
    }]
  }
};
Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57