如何使用backbone.js划分JSON收集结果
How to divide JSON collection result using backbone.js
当前我的json的结果是这样的。
<div id="item-list">
<div class="row" id="featured-item">
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
</div>
</div>
可以这样做吗?
<div id="item-list">
<div class="row">
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
</div>
<div class="row">
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
</div>
<div class="row">
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
</div>
<div class="row">
<div class="col-md-6"><p>Content Righ Here</p></div>
<div class="col-md-6"><p>Content Righ Here</p></div>
</div>
<div class="row">
<div class="col-md-6"><p>Content Righ Here</p></div>
</div>
</div>
这是我当前的jS代码:
var Feed = Backbone.Model.extend({
url: function () {
//API LINK HERE
},
defaults: {
// SOME ATTRIBUTES HERE
}
});
var Stream = Backbone.Collection.extend({
url: function () {
//API LINK HERE
return link;
},
page: 0,
sort: 'date_created',
order: 'desc',
limit: 12,
model: Feed,
parse: function (response) {
$('.total_records').text(response._count);
var tags = response.data;
this.totalRecords = response._count;
return tags;
}
});
// Master view
var DirectoryView = Backbone.View.extend({
el: $("#main-section"),
initialize: function () {
this.collection = this.options.collection;
this.isLoading = false;
this.render();
this.collection.on("add", this.renderFeed, this);
},
events: {
'click #loadmore-feed' : 'loadMore'
},
render: function () {
var that = this;
this.isLoading = true;
this.collection.fetch({success: function () {
_.each(that.collection.models, function (item) {
that.renderFeed(item);
}, this);
that.isLoading = false;
$("abbr.timeago").timeago();
that.$el.find('#load-more-container').html(
_.template($('#load-more-template').html())
);
}
});
if(!this.collection.totalRecords) {
$('#load-more-container').hide();
}
},
loadMore: function () {
$('#load-more-container').hide();
if(this.collection.totalRecords > this.collection.page) {
this.collection.page += 12;
this.render();
$('#load-more-container').show();
}
},
renderFeed: function (item) {
var optionsView = new OptionsView({
model: item
});
this.$el.find('#item-list > div').append($(optionsView.render().el).hide().fadeIn('slow'));
}
});
var OptionsView = Backbone.View.extend({
tagName: "div",
className: "col-md-6",
template: $("#emp-list-item").html(),
render: function () {
var tmpl = _.template(this.template);
this.$el.html(tmpl(this.model.toJSON()));
return this;
}
});
HTML代码
<div id="main-section">
<div id="item-list">
<div class="row" id="featured-item"></div>
</div>
<div id="load-more-container"></div>
</div>
<!-- backbonejs view template -->
<script id="emp-list-item" type="text/template">
<p>Content Righ Here</p>
</script>
<script id="load-more-template" type="text/template">
<p>
<div class="progress xs progress-striped">
<div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 100%">
<span class="sr-only">60% Complete (warning)</span>
</div>
</div>
<div class="tcenter">
<a href="javascript:void(0)" id="loadmore-feed">Load more</a>
</div>
</p>
</script>
<!-- Script for autoloading on mobile device -->
<script type="text/javascript">
$(document).ready(function() {
var paginatedItems = new Stream();
var directoryView = new DirectoryView({collection: paginatedItems});
$(window).scroll(function() {
if (directoryView.$el.is(':visible') && !directoryView.isLoading && $(window).scrollTop() + $(window).height() > getDocHeight() - 400 ) {
directoryView.loadMore();
}
});
});
</script>
你的问题标题涉及JSON,但你的例子涉及HTML,这是完全不同的问题。我不知道您的意思是"我能产生这种类型的HTML结构吗?"还是"我能让我的集合保存这种数据结构吗?"
在不解决核心问题的情况下,您仍然应该将fetch
移出render
,并使render
发生在success
上。例如:
initialize: function(models) {
// ...do stuff...
if (!models)
this.fetch({success: this.render, error: this.error});
_.bindAll(this, 'render', 'error', 'render');
},
在你的success
,你应该准备render
:
render: function(collection) {
var modelsFlat = collection.models;
var nested = _(list).chain().partition(function(x) {
// below makes an array of evens and odds indices
return (_(list).indexOf(x) % 2) == 0;
}).zip() // this turns columns into rows, ie [[1,3,5],[2,4,6]] => [[1,2],[3,4],[5,6]]
.value(); // this unwraps (no more chaining)
_(nested).each(function(n) {
var inner = new Backbone.Collection(n);
var innerView = new OptionsView(inner);
innerView.render();
});
// Do the rest of your current render
}
你的新DirectoryItemView
和OptionsView
差不多,不涉及服务器端的东西。
:
- 在外部视图的
initialize
上调用fetch
来获取外部集合 -
success
呼叫render
- 在
render
上,将每对物品重新组织成一个新的小集合 - 在每个集合上,用该集合创建一个新的内部
OptionsView
,并渲染它 - 完成外部视图的渲染。
相关文章:
- JSON.stringify和Object.keys在同一个对象上产生不同的结果
- 对于每个json结果,使用类jquery添加css
- 从JSON API结果迭代时未定义
- 在Ionic'中显示JSON结果;s系列重复
- 无法将JSON.parse与JSON_encode中的结果一起使用
- 如何将JSON结果设置为'src'共'img'以在ASP.NET MVC4中显示图像
- json-api和错误结果:无法读取属性'长度'的未定义
- JSON返回数组的奇怪结果
- 获取json结果的身份验证问题
- Neo4j结果(JSON格式)创建D3树图
- 无法在AJAX成功访问中显示JSON结果
- 返回带有筛选结果的新JSON对象
- (临时)在Web应用程序中存储JSON搜索结果
- 迭代 JSON 文件并且未在 Javascript 中正确返回结果
- 意外结果,在 ASP.Net 中解析 JSON 对象的数组
- jquery jqgrid 不显示结果,具体取决于 JSON 对象中的参数
- 从Node.js上的mysql结果JSON中获取一个Array值
- 搜索框结果 == JSON 结果
- 正在从自动完成结果Json中删除重复项
- 如何合并两个facebook图api数据结果(JSON)