主干视图删除其他实例
Backbone view removes other instances
在测试backbone.js时,我遇到了一个问题。如何创建一个可以在页面上多次使用的通用视图?
我目前的实现通过使用Jquery的append
创建了一个简单的FooControls
视图,用于其他两个。
然而,它最终只在最后一个视图中被使用并添加到DOM中。
JSBin,再现我的问题。http://jsbin.com/kopos/1/edit(红色方块代表FooControls
视图,不应该有两个红色方块吗?)
我该如何创建一个可以在页面上多次使用的视图?
试试这个(参见jsbin示例):
var FooControls = Backbone.View.extend({
className: 'foo-control'
});
var ScreenView = Backbone.View.extend({
className: 'screen',
render: function() {
var fooOne = new FooControls();
this.$el.append(fooOne.$el);
}
});
var FileView = Backbone.View.extend({
className: 'files',
render: function() {
var fooTwo = new FooControls();
this.$el.append(fooTwo.$el);
}
});
var screenInstance = new ScreenView();
var fileInstance = new FileView();
screenInstance.render();
fileInstance.render();
$('#wrapper').append(screenInstance.$el);
$('#wrapper').append(fileInstance.$el);
使用className
可以获得您似乎正在寻找的相同功能,而不会出现渲染问题。
var FooControls = Backbone.View.extend({
className: 'foo-control'
});
var ScreenView = Backbone.View.extend({
className: 'screen',
render: function() {
var fooOne = new FooControls();
this.$el.append(fooOne.$el);
return this;
}
});
var FileView = Backbone.View.extend({
className: 'files',
render: function() {
var fooTwo = new FooControls();
this.$el.append(fooTwo.$el);
return this;
}
});
var screenInstance = new ScreenView(),
fileInstance = new FileView();
$('#wrapper')
.append(screenInstance.render().$el);
.append(fileInstance.render().$el);
相关文章:
- ES6构造函数返回基类的实例
- 如何添加浮动和非浮动,其他
- 与其他库的jQuery.noConflict()
- jQuery"焦点”;在一个实例中有效,但在其他实例中无效
- 可以获取模板实例值,但不能将其用于其他函数
- Aurelia组件在其他视图模型中使用时不共享实例
- 在 PHP 中,当同一页面的其他实例更改时,更新页面
- 主干.js:如何在集合/其他模型中实例化模型
- Selenium Webdriver实例上的JavascriptExecutor在导航到其他页面时引发异常
- 使用其他javascript删除字符串中某个字符的所有实例
- 登录到其他选项卡上的一个Web浏览器实例中的应用程序
- 当指令的其他实例中的模型发生更改时,检查函数
- 如何使用RtcPeerConnection连接其他实例
- 主干视图删除其他实例
- 如何在不中断同一插件的其他实例的情况下取消订阅事件
- 如何做:AngularJS服务或其他组件在模块加载时实例化
- NodeJS与其他NodeJS实例通信并管理minecraft服务器
- 您可以使用猫鼬中的实例方法搜索其他模型吗?
- Angularjs - 使用指令实例化其他指令
- 在其他类之间共享两个实例