可以在backbone.js中将子视图附加到网格中一次
Possible to append subviews once to grid in backbone.js?
我正在通过服务onReady加载JSON数据(对象数组),并希望在网格中显示这些数据。我用一个视图表示每一行。
render: function(){
var self = this;
(self.collection.models).forEach( function(model){
var rowView = new RowView({model: model});
self.$el.append(rowView.render().el);
});
}
是否可以构建子视图并将它们一次推送到DOM,而不是一乘一?浏览器是否回流&每次追加都要重新绘制?
我见过人们添加子视图/子视图的所有方法,但都没有解决问题(频繁的DOM访问?),因为这就是构建主干网的方式?
是的,这是可以做到的。使用jQuery生成一个html元素(使用视图的tagName
定义和attributes
等等),然后将所有内容附加到该元素。完成后,将当前的this.$el
换成新的:
render: function(){
// create in memory element
var $el = $(this.tagName);
// also get the `className`, `id`, `attributes` if you need them
// append everything to the in-memory element
this.collection.each(function(model){
var rowView = new RowView({model: model});
$el.append(rowView.render().el);
});
// replace the old view element with the new one, in the DOM
this.$el.replaceWith($el);
// reset the view instance to work with the new $el
this.setElement($el);
}
这样就可以了。
当然,你会在屏幕上看到一点闪烁,这取决于浏览器的速度和变化的大小。但这应该会让你走上做你想做的事的道路。
有关replaceWith的详细信息:http://api.jquery.com/replaceWith/
我认为有一个比@Derick的答案更简单的解决方案:
render : function () {
var children = [];
this.collection.forEach( function( model ) {
var rowView = new RowView( { model : model } );
children.push( rowView.render().el );
} );
this.$el.empty().append( children );
return this;
}
http://jsfiddle.net/tXnTk/
在@Derick的回答中对代码的评论(我的评论标记为"[JMM]:"):
render: function(){
// [JMM]: You should already have this.el / this.$el, so there
// [JMM]: should be no need to mess with this.tagName, className,
// [JMM]: id, etc. Just store the child views in an array.
// create in memory element
var $el = $(this.tagName);
// also get the `className`, `id`, `attributes` if you need them
// append everything to the in-memory element
this.collection.each(function(model){
var rowView = new RowView({model: model});
$el.append(rowView.render().el);
});
// [JMM]: Using replaceWith will wipe out event listener
// [JMM]: registrations and other data associated with the
// [JMM]: DOM element. That's why it's necessary to call
// [JMM]: setElement() afterward. Since you already have
// [JMM]: the DOM element and you just want to change the
// [JMM]: content you might as well just empty it out and
// [JMM]: append the new children.
// replace the old view element with the new one, in the DOM
this.$el.replaceWith($el);
// reset the view instance to work with the new $el
this.setElement($el);
}
相关文章:
- Javascript返回值只在循环中返回一次
- Jquery FadeIn FadeOut 只工作一次
- Javascript html每点击一次就会更改url
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- jQuery滚动功能只工作一次
- 刷新导致我的帖子“;张贴“;再一次
- 引导程序崩溃一次只能看到一个
- 有没有一个抽象层,这样我就可以集成一次,然后使用pusher、pubnub或faye
- 加载器组件仅加载一次
- 根据Angular.JS上一次的内容禁用选择
- 一次又一次地在新的和相同的选项卡中打开一个url
- 只在宽度以下和宽度以上各准备一次
- 如果30秒未单击,请应用CSS一次,将其删除,然后重新迭代
- 通过一次提交保存来自多个 ASP.NET Telerik 网格的更改
- 可以在backbone.js中将子视图附加到网格中一次
- 一次将网格视图数据从客户端发送到服务器端
- 在网格视图内(单击时标签转到文本框)一次一个
- jqGrid:网格函数只执行一次
- KendoUI网格在一次刷新中更改过滤器和排序