模板我有一個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"
}
}
我使用的骨幹,我要訪問的子元素(效果圖)。 我覆蓋瞭解析方法,告訴我的'渲染'是一個新的集合,這部分工作正常。 我遇到一些問題的時候是我將我的問題集合傳遞給我的模板。然後,我可以針對每個問題和訪問他的屬性(ID,發佈,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;
});
然後,我有一個很基本的模板。
要恢復,我有一個包含RENDERINGS集合的模型ISSUE的集合問題,我想訪問模板中的模型RENDER。
編輯: IssueCollection
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;
});
IssueModel
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;
});
RenderingsCollection
define([
'underscore',
'backbone',
'models/rendering/RenderingModel'
], function(_, Backbone, RenderingModel){
var RenderingsCollection = Backbone.Collection.extend({
model: RenderingModel
//initialize : function(models, options) {},
});
return RenderingsCollection;
});
RenderingModel
define([
'underscore',
'backbone'
], function(_, Backbone) {
var RenderingModel = Backbone.Model.extend({
return RenderingModel;
});
感謝的!
我看不到模型RENDER,它是_renderings_的父對象? – 2013-04-08 13:05:18
*我有這樣的JSon,這是一個問題的集合:*我會說這只是一個問題,而不是一個集合。 – Loamhoof 2013-04-08 13:05:32
@Loamhoof,這只是第一個,還有很多其他的。 – brunettia 2013-04-08 13:08:54