我有一个像这样的 JSON,它是一系列问题:
{
"id": "505",
"issuedate": "2013-01-15 06:00:00",
"lastissueupdate": "2013-01-15 13:51:08",
"epub": false,
"pdf": true,
"price": 3,
"description": "Diese Version ist nur als PDF verfügbar.",
"downloadable": true,
"renderings": [
{
"height": 976,
"width": 1024,
"sourcetype": "pdf",
"pagenr": 0,
"purpose": "newsstand",
"relativePath": null,
"size": 0,
"url": "a/url/image.jpg"
},
{
"height": 488,
"width": 512,
"sourcetype": "pdf",
"pagenr": 0,
"purpose": "newsstand",
"relativePath": null,
"size": 0,
"url": "a/url/image.jpg"
}
}
我正在使用主干,我想访问子元素(渲染)。我覆盖了 parse 方法来告诉我我的“渲染”是一个新的集合,这部分工作正常。当我在模板中传递我的问题集合时,我遇到了一些问题。然后我可以为每个问题制作一个并访问他的属性(id、issuedate、lastissueupdate 等),但是我怎样才能访问我的问题的渲染?
我的观点:
define([
'jquery',
'underscore',
'backbone',
'text!templates/home/homeTemplate.html',
'collections/issues/IssueCollection'
], function($, _, Backbone, homeTemplate, IssueCollection){
var HomeView = Backbone.View.extend({
el: $("#page"),
initialize:function(){
var that = this;
var onDataHandler = function(issues){
that.render();
}
this.issues = new IssueCollection();
this.issues.fetch({
success:onDataHandler,
error:function(){
alert("nicht gut")
}
});
},
render: function(){
$('.menu li').removeClass('active');
$('.menu li a[href="#"]').parent().addClass('active');
var compiledTemplate = _.template(homeTemplate, {_:_, issues: this.issues.models});
this.$el.html(compiledTemplate);
}
});
return HomeView;
});
然后我有一个非常基本的模板。
要恢复,我有一个模型 ISSUE 的集合问题,其中包含一个渲染集合,我想在我的模板中访问模型渲染。
编辑: 问题集合
define([
'underscore',
'backbone',
'models/issue/IssueModel'
], function(_, Backbone, IssueModel){
var IssueCollection = Backbone.Collection.extend({
model: IssueModel,
url: function(){
return '/padpaper-ws/v1/pp/fn/issues';
},
parse: function(response){
return response.issues;
}
//initialize : function(models, options) {},
});
return IssueCollection;
});
问题模型
define([
'underscore',
'backbone',
'collections/renderings/RenderingsCollection'
], function(_, Backbone, RenderingsCollection) {
var IssueModel = Backbone.Model.extend({
parse: function(response){
response.renderings = new RenderingsCollection(response.renderings);
return response;
},
set: function(attributes, options) {
if (attributes.renderings !== undefined && !(attributes.renderings instanceof RenderingsCollection)) {
attributes.renderings = new RenderingsCollection(attributes.renderings);
}
return Backbone.Model.prototype.set.call(this, attributes, options);
}
});
return IssueModel;
});
渲染集合
define([
'underscore',
'backbone',
'models/rendering/RenderingModel'
], function(_, Backbone, RenderingModel){
var RenderingsCollection = Backbone.Collection.extend({
model: RenderingModel
//initialize : function(models, options) {},
});
return RenderingsCollection;
});
渲染模型
define([
'underscore',
'backbone'
], function(_, Backbone) {
var RenderingModel = Backbone.Model.extend({
return RenderingModel;
});
谢谢 !!