【发布时间】:2014-10-22 08:17:37
【问题描述】:
从主视图中,我尝试渲染一些应驻留在主视图页面中的子视图,例如成绩视图及其子视图成绩视图,当单击导航栏图标时,一个可见并填满整个页面。主页有一个导航栏,其中包含一次显示此子视图的链接。主页视图类似于带有导航栏的 Facebook 移动应用程序页面,并且子视图(如提要、好友请求等)填充导航栏下的页面。
我已经实现了其中一个子视图,但无法渲染它。 Homeview 成功渲染。每个子视图在初始化时呈现自己,而成绩集合在初始化时获取自己。
主页:
define([
'jquery',
'ratchet',
'underscore',
'backbone',
'text!home/hometemplate.html',
'grades/gradesview',
],
function($, Ratchet, _, Backbone, HomeTemplate, GradesView){
var HomeView = Backbone.View.extend({
el: $('body'),
initialize: function() {
//X-construct the subviews in initialize to create them once.
this.gradesView = new GradesView();
this.render();
},
template: _.template( HomeTemplate ),
render: function() {
this.$el.html( this.template() );
//X-Place the Subview element into DOM.
this.$('#gradesviewcontainer').html( this.gradesView.el );
},
});
return HomeView;
})
成绩视图:
define([
'underscore',
'backbone',
'grades/gradescollection',
'grades/gradeview',
],
function(_, Backbone, GradesCollection, GradeView){
var GradesView = Backbone.View.extend({
el: $('#gradescontainer'),
//model: GradeModel,
initialize: function(){
this.collection = new GradesCollection;
this.collection.each(function(item) {
console.log(item);
});
this.render();
},
//template:,
render: function() {
this.collection.each( function (item) {
this.renderGrade( item );
}, this );
},
renderGrade: function(item) {
this.gradeView = new GradeView( {model: item} );
this.$el.append( this.gradeView.el );
},
});
return GradesView;
})
成绩单:
define([
'underscore',
'backbone',
'grades/grademodel',
],
function(_, Backbone, GradeModel) {
var GradesCollection = Backbone.Collection.extend ({
model: GradeModel,
url: '/grade',
initialize: function() {
this.fetch();
},
});
return GradesCollection;
});
成绩:
define([
'underscore',
'backbone',
'grades/grademodel',
'text!grades/gradetemplate.html'
],
function(_, Backbone, GradeModel, GradeTemplate){
var GradeView = Backbone.View.extend({
tagName: 'div',
initialize: function(){
this.render();
},
template: _.template( GradeTemplate ),
render: function() {
this.$el.html( this.template( this.model.attributes ));
return this;
},
});
return GradeView;
})
这里的另一个问题是导航栏链接每个 html 锚点在 homeview 渲染中不起作用。
在 JS 控制台中,我可以看到从我的服务器响应的资源 json,即预期的对象数组:[{"coursename":"Math","gradenumeric":100},{"coursename":"Phys","gradenumeric":80}]。我在这里缺少什么?
【问题讨论】: