Javascript BackboneJS渲染问题
在过去的六个月里,我一直在用脊梁骨工作。前两个月,我一直在胡思乱想,学习并弄明白如何围绕它构建代码。在接下来的4个月里,一个适合生产的应用程序被大量使用。不要误解我的意思,主干将我从以前标准的数千行混乱的客户端代码中解救出来,但它使我能够在更短的时间内完成更宏大的事情,带来了一系列全新的问题。对于我在这里提出的所有问题,有一些简单的解决方案,感觉像是黑客,或者只是感觉错了。我保证一个很棒的解决方案会得到300分的奖励。下面是:Javascript BackboneJS渲染问题,javascript,jquery,node.js,backbone.js,coffeescript,Javascript,Jquery,Node.js,Backbone.js,Coffeescript,在过去的六个月里,我一直在用脊梁骨工作。前两个月,我一直在胡思乱想,学习并弄明白如何围绕它构建代码。在接下来的4个月里,一个适合生产的应用程序被大量使用。不要误解我的意思,主干将我从以前标准的数千行混乱的客户端代码中解救出来,但它使我能够在更短的时间内完成更宏大的事情,带来了一系列全新的问题。对于我在这里提出的所有问题,有一些简单的解决方案,感觉像是黑客,或者只是感觉错了。我保证一个很棒的解决方案会得到300分的奖励。下面是: 加载-对于我们的用例(管理面板),悲观同步是不好的。对于某些事情,我
window.old_sync = Backbone.sync
# Add a loading event to backbone.sync
Backbone.sync = (method, model, options) ->
old_sync(method, model, options)
model.trigger("loading")
太好了。它按预期工作,但感觉不正确。我们将此事件绑定到所有相关视图,并显示加载图标,直到从该模型接收到成功或错误事件。有更好、更理智的方法吗
现在,对于那些困难的人:
-
:
注意类模型--name
,这将是我们的注入点
然后,我们可以定义一个基本视图(或修改Backbone.view),以便在更新这些节点时使用适当的值填充它们:
var V=Backbone.View.extend({
初始化:函数(){
//将所有更改绑定到集合中的模型
this.collection.on('change',this.autoupdate,this);
},
//抓取更改并填充任何区域集以接收值
自动更新:功能(模型){
var_this=这个,
changes=model.changedAttributes(),
属性=\键(更改);
_.每个(属性,函数(属性){
_这个.$('.model-'+model.cid+'-'+attr.html(model.get(attr));
});
},
//呈现完整的模板
//只有当视图真正发生戏剧性变化时才应该发生
渲染:函数(){
var数据,html;
//生成数据以呈现模板
//实际上,添加了cid的.collection.toJSON()
data=this.collection.map(函数(模型){
返回u2;.extend(model.toJSON(),{cid:model.cid});
});
html=模板({子项:数据});
这个.$el.html(html);
归还这个;
}
});
代码会有所不同,以适应模型而不是集合。
小提琴
限制DOM操作
将呈现委托给子视图可能代价高昂,它们的HTML片段必须插入父视图的DOM中。
看看这个
其要点是,生成完整的HTML结构并应用视图要比构建视图和子视图快得多
//Container view
init: function() {
this.collection = new Backbone.Collection({
url: 'http://mybestpage.com/collection'
});
this.collection.bind('change', this.render, this);
this.collection.fetch();
},
render: function() {
_.each(this.collection.models, function(model) {
var newView = new myItemView({
model: model,
name: 'view' + model.id
});
this.$('#my-collection').append(newView.render().$el);
view.on('viewEdit', this.displayValue);
}, this);
},
...
displayValue: function(value) {
//method 1
this.displayView.setText(value); //we can create little inner view before,
//for text displaying. Сonvenient at times.
this.displayView.render();
//method 2
$(this.el).find('#display').html(value);
}
//View from collection
myItemView = Backbone.View.extend({
events: {
'click #edit': 'edit'
},
init: function(options) {
this.name = options.name;
},
...
edit: function() {
this.trigger('viewEdit', this.name, this);
}
var smartView = Backbone.View.extend({
initialize: function(){
this.model.on( "imageUpdate", this.imageUpdate, this );
this.model.on( "contentUpdate", this.contentUpdate, this );
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
},
imageUpdate: function(){
this.$el.find('#image').attr('src', this.model.get('imageUrl'));
},
contentUpdate: function(){
this.$el.find('#content').html(this.model.get('content'));
}
})
Organization model
|--> Event model
|--> News model
|--> Comment model