【发布时间】:2023-03-05 19:26:01
【问题描述】:
列表视图
define(['jquery', 'underscore', 'backbone', 'text!views/abcView/abcListView.html','views/abcView/ListTemplate' ,'app/utils', 'collection/abcListCollection'], function($, _, Backbone, tmpl_abcummaryView, abcListView, Utils, abcListCollection) {
var abcListView = Backbone.View.extend({
// Setting the view's template property using the Underscore template method
template: _.template(tmpl_abcummaryView),
// View constructor
initialize: function() {
mainRouter.collections.abc = new abcListCollection();
},
// View Event Handlers
events: {
},
// Renders the view's template to the UI
render: function() {
var self=this;
this.$el.html(this.template({data: this.templateData}));
mainRouter.collections.abc.fetchData({
success: function (collection, response) {
_.each(collection, function (obj) {
html = new abcListView({model: obj}).render();
self.$el.find('#abcList').append(html);
})
},
error: function (err) {
console.log("error");
}
});
// Maintains chainability
return this;
}
});
return abcListView;
});
收藏
define(['underscore', 'backbone', 'models/abcModel', 'app/utils'], function(_, Backbone, abcModel, Utils) {
var self;
//List of Alerts stored in Backbone Collection
abcListCollection = Backbone.Collection.extend({
model: abcSummaryModel,
initialize: function() {
self = this;
_.bindAll(this, 'fetchData');
},
fetchData: function(obj) {
add=true;
var data = {
"method": "method name",
"params": {
param1:"param1",
param2:"param2"
}
}
Utils.Ajax.post(Utils.WebAPI.WebAPIServer, data, function(response, textStatus, jqXHR) {
obj.success.call(self.collection, response);
}, 'json', function(err) {
console.log(JSON.stringify(err));
obj.error.call(err);
}, "loading");
},
collection: {}
});
return abcListCollection;
});
如何延迟加载集合,即最初显示 5 个项目,当用户滚动屏幕时获取下 5 个记录?
【问题讨论】:
-
对于初始数据,您可以将这些项目提升到您的集合中,然后当您在 page parameter 中执行获取传递时。
-
你能给我举个例子吗?
-
我应该可以做到。您似乎没有使用骨干网的 fetch 来获取您的集合数据,对吗?
-
是的,我没有使用骨干网的提取,因为我想将数据发布到服务器,然后响应将可用。而且我不知道是否可以使用 fetch 将数据发布到服务器?
-
一般来说,你可以覆盖主干的同步方法来发出你想要的请求,但更具体地说,只是设置emulateHttp 可能会做你想做的事。那就是说你明白我的回答了吗?
标签: backbone.js lazy-loading backbone.js-collections