ember.js无法从元数据中获取分页

时间:2013-08-20 02:11:51

标签: ember.js pagination metadata

我做了一个像this这样的例子,但仍然无法获得分页

这是我的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'

这是我的控制器

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')

这是回复

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

这是我的日志:

  

Eme.Plugin

     

对象{}

1 个答案:

答案 0 :(得分:2)

在您粘贴的示例中,modelType变量在定义之前输出到控制台。这可能就是为什么你没有按预期看到分页数据的原因。

我创建了一个带有稍微修改过的代码版本的jsbin,它似乎可以正确输出分页数据。请参阅: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"
    }]
  }
};