admin管理员组文章数量:1323330
I am using Marionette.CompositeView
and I would like to understand the difference between serializeData and onRender
based on these two exemple (1) and (2).
According the documentation serializeData is called in render before applying the template, and onRender is called in render after applying the template.
My questions are:
1) why the example (1) works and the (2) does not?
2) If I reset the collection, will the Marionette.CompositeView
be re-rendered?
Please see the ments in the code for more details.
(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?
}
});
I am using Marionette.CompositeView
and I would like to understand the difference between serializeData and onRender
based on these two exemple (1) and (2).
According the documentation serializeData is called in render before applying the template, and onRender is called in render after applying the template.
My questions are:
1) why the example (1) works and the (2) does not?
2) If I reset the collection, will the Marionette.CompositeView
be re-rendered?
Please see the ments in the code for more details.
(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?
}
});
Share
edited Aug 10, 2012 at 8:04
Lorraine Bernard
asked Aug 9, 2012 at 22:42
Lorraine BernardLorraine Bernard
13.4k23 gold badges85 silver badges138 bronze badges
1 Answer
Reset to default 61) As you said, onRender
is just a callback function which is called after rendering the view.
serializeData
must return a valid JSON object as it's said in the 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, the answer is yes. In the Backbone Marionette documentation, it's said :
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.
本文标签: javascriptDifference between serializeData and onRender in MarionetteCompositeViewStack Overflow
版权声明:本文标题:javascript - Difference between serializeData and onRender in Marionette.CompositeView - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742140052a2422541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论