gpt4 book ai didi

javascript - 结果总是分页时的服务器端分页

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:06 25 4
gpt4 key购买 nike

我正在学习 Ember,我正在尝试做一个分页列表,从 RESTful 服务中检索数据。

服务返回数据如下:

{
    result: [
        {
            id: " ALFKI "
            companyName : " Alfreds Futterkiste "
            contactName : " Maria Anders " ,
            ContactTitle : " Sales Representative "
            address : " Obere Str 57" ,
            city: "Berlin " ,
            postalCode : " 12209 " ,
            country: " Germany" ,
            phone: " 030-0074321 "
            fax: " 030-0076545 "
            link: " http://localhost:2828/customers/ALFKI "
        }
        {
            id: " ANATR "
            companyName : " Ana Trujillo sandwiches and ice cream "
            contactName : " Ana Trujillo " ,
            ContactTitle : "Owner " ,
            address : " 2222 Avenue of the Constitution " ,
            city: " Mexico DF "
            postalCode : " 05021 " ,
            country: " Mexico " ,
            phone: " (5) 555-4729 "
            fax: " (5) 555-3745 "
            link: " http://localhost:2828/customers/ANATR "
        }

        ]
        metadata : {
            offset : 1,
            limit : 10,
            totalCount : 92,
            links: {
                self " http://localhost:2828/customers?format=json&offset=1&limit=10 "
                last: " http://localhost:2828/customers?format=json&offset=82&limit=10 "
                next : " http://localhost:2828/customers?format=json&offset=11&limit=10 "
            }
    }
}

服务器总是返回带有分页数据的集合作为链接。要请求新页面,URL 具有以下格式:

http://api.server/customers?offset=1&limit=10

我的路由配置是:

Northwind.CustomersRoute = Ember.Route.extend({    

model: function () {

var controller = this.controllerFor('customer');

return this.get('store').findQuery('customer', {offset: controller.offset, limit:controller.limit});

}

});

如何为这种情况实现 Controller ?

提前致谢。

最佳答案

已解决

新路由器

Northwind.CustomersRoute = Ember.Route.extend({        
model: function () {
var controller = this.controllerFor('customer');

return this.get('store').findQuery('customer', { offset: controller.offset, limit: controller.limit });
},

setupController: function (controller, model) {
controller.set('model', model);
controller.set('contentLoaded', true);
}
});

我正在使用类似于 this 的分页混合.

Controller

Northwind.ArrayController = Ember.ArrayController.extend(Northwind.PaginationMixin, {        
contentLoaded: false,
loadMetadata: function () {
if (!this.get('contentLoaded')) return;

var model = this.get('model');
var meta = this.get('store').metadataFor(model.type);

if (meta) {
// Metadata object
var metadata = Ember.Object.create({
offset: meta.offset,
limit: meta.limit,
totalCount: meta.totalCount,
links: Ember.makeArray()
});

// Pagination links
if (meta.links) {
for (var link in meta.links) {
var lnkObj = Ember.Object.create(Northwind.Common.Uri.queryParams(meta.links[link]));
lnkObj.set('rel', link);
metadata.links.pushObject(lnkObj);
}
}

this.set('metadata', metadata);
this.set('limit', metadata.limit);
this.set('totalCount', metadata.totalCount);
// offset is automatically calculated based on page property
}

}.observes('contentLoaded'),

refresh: function () {
var offset = this.get('offset');
var limit = this.get('limit');
var self = this;

self.set('contentLoaded', false);

this.get('store').findQuery('customers', { offset: offset, limit: limit }).then(function (result) {
self.set('model', result);
self.set('contentLoaded', true);
});
}.observes('page')
});

https://github.com/jdmartinez/Northwind/tree/pre-0.1

关于javascript - 结果总是分页时的服务器端分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19253097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com