[英]Backbone.js Subview rendering
I am trying to get to grips with Backbone.js and have been following the 'modular' approach outlined by Thomas Davis here. 我试图与Backbone.js保持联系,并一直遵循Thomas Davis在此处概述的“模块化”方法。
I seem to be stuck when trying to 'include' a view within another view as follows: 尝试将一个视图“包含”在另一个视图中时,我似乎陷入困境:
SettingsView.js SettingsView.js
define([
'jquery',
'underscore',
'backbone',
'text!templates/settings/settingsTemplate.html'
], function($, _, Backbone, settingsTemplate){
var SettingsView = Backbone.View.extend({
el: $("#interface"),
render: function() {
this.$el.html(settingsTemplate);
}
});
return SettingsView;
});
ScriptView.js ScriptView.js
define([
'jquery',
'underscore',
'backbone',
'views/settings/SettingsView',
'models/script/ScriptModel',
'collections/scripts/ScriptsCollection',
'text!templates/script/scriptTemplate.html'
],
function($, _, Backbone, SettingsView, ScriptModel, ScriptsCollection, scriptTemplate) {
var ScriptView = Backbone.View.extend({
el : $("#container"),
render : function() {
this.$el.html(scriptTemplate);
//add the settings view
var settingsView = new SettingsView();
settingsView.render();
}
});
return ScriptView;
});
UPDATED: I have managed to fix the error which I have just realised as to why that was happening (params were in the wrong order - DOH!!) 更新:我已经设法解决了我刚刚意识到的错误原因(参数顺序错误-DOH!)
I no longer get the error but my 'SettingsView' is still not appearing. 我不再收到错误,但是我的“ SettingsView”仍然没有出现。 When I console log this inside 'ScriptView.js' I see that 'el' is undefined so my thinking is that this is where the issue may be...? 当我在'ScriptView.js'中进行控制台记录时,我看到'el'是未定义的,所以我想这就是问题所在。
Thanks 谢谢
I think you have to append the SettingsView
to the el
of the ScriptView
: 我认为您必须将SettingsView
附加到ScriptView
的el
:
var ScriptView = Backbone.View.extend({
el : $("#container"),
render : function() {
this.$el.html(scriptTemplate);
//add the settings view
var settingsView = new SettingsView();
settingsView.render();
this.$el.append(settingsView.el);
}
});
And a great post about subviews is this one . 而关于子视图一个伟大的职位是这一个 。
I hope it helps! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.