gpt4 book ai didi

javascript - 无限滚动 Backbone View

转载 作者:行者123 更新时间:2023-11-29 15:45:10 28 4
gpt4 key购买 nike

我想要从 JSON 提要中呈现无限/无休止的滚动数据。我有兴趣使用 Backbone/Underscore/jQuery 完成类似于 Pinterest 或 Google Reader 的事情。

我如何申请 infiniScroll.js模块到我的主干 View ? 目标是在您滚动到页面末尾附近时获取并附加下一页的(“页面”URL 参数)推文。问题:到达页面底部时,获取相同的 JSON 页面提要。如何将URL中的page参数改为&page=2

演示: http://dl.dropbox.com/u/19974044/test.html或者 http://jsfiddle.net/k4rPP/3/

// Define the model
Tweet = Backbone.Model.extend();

// Define the collection
Tweets = Backbone.Collection.extend({
model: Tweet,
// Url to request when fetch() is called
url: 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&trim_user=false&count=10&screen_name=cnn&page=1&callback=?',
parse: function (response) {
return response;
},
// Overwrite the sync method to pass over the Same Origin Policy
sync: function (method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: false
}, options);

return $.ajax(params);
}
});

// Define the View
TweetsView = Backbone.View.extend({
initialize: function () {
_.bindAll(this, 'render');
// create a collection
this.collection = new Tweets;
// Fetch the collection and call render() method
var that = this;
this.collection.fetch({
success: function () {
that.render();
}
});
// infiniScroll.js integration
this.infiniScroll = new Backbone.InfiniScroll(this.collection, {success: this.appendRender, param:'page', includePage:true});
},
// Use an extern template
template: _.template($('#tweetsTemplate').html()),

render: function () {
// Fill the html with the template and the collection
$(this.el).html(this.template({
tweets: this.collection.toJSON()
}));
}
});

var app = new TweetsView({
// define the el where the view will render
el: $('body')
});​

最佳答案

url 属性可以指定为函数而不是字符串。所以你可以用这样的东西代替它:

...
currentPage: 0,
url: function() {
this.currentPage++;
return 'https://path.to.url/?page=' + this.currentPage;
},
...

关于javascript - 无限滚动 Backbone View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12679169/

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