gpt4 book ai didi

javascript - Marionette.CompositeView 中 serializeData 和 onRender 的区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:49 25 4
gpt4 key购买 nike

我正在使用 Marionette.CompositeView我想了解 serializeData 和 onRender 之间的区别基于这两个例子(1)和(2)。

根据文档,在应用模板之前在渲染中调用 serializeData,在应用模板之后在渲染中调用 onRender。

我的问题是:
1) 为什么示例 (1) 有效而示例 (2) 无效?
2) 如果我重置集合,Marionette.CompositeView 会被重新渲染吗?

更多细节请查看代码中的注释。


(1)

return Marionette.CompositeView.extend({

initialize: function () {
this.collection = new MyCollection();
this.collection.fetch();
},

onRender: function () {
this.collection.length > 0 ? this.$el.show() : this.$el.hide();
// it returns this.collection.length > 0
// differently from serializeData.
}
});

(2)

return Marionette.CompositeView.extend({

initialize: function () {
this.collection = new MyCollection();
this.collection.fetch();
},

serializeData: function () {
this.collection.length > 0 ? this.$el.show() : this.$el.hide();
// it returns this.collection.length = 0
// even if this.collection.length > 0. Why?
}
});

最佳答案

1) 如您所说,onRender 只是一个回调函数,在渲染 View 后调用。

serializeData 必须返回有效的 JSON 对象,如 Backbone Marionette documentation 中所述:

If you need custom serialization for your data, you can provide a serializeData method on your view. It must return a valid JSON object, as if you had called .toJSON on a model or collection.

Backbone.Marionette.ItemView.extend({
serializeData: function(){
return {
"some attribute": "some value"
}
}

});

2) IMO,答案是肯定的。在Backbone Marionette documentation ,据说:

CollectionView: Automatic Rendering

The collection view binds to the "add", "remove" and "reset" events of the collection that is specified.

When the collection for the view is "reset", the view will call render on itself and re-render the entire collection.

When a model is added to the collection, the collection view will render that one model in to the collection of item views.

When a model is removed from a collection (or destroyed / deleted), the collection view will close and remove that model's item view.

关于javascript - Marionette.CompositeView 中 serializeData 和 onRender 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11893057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com