- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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/
问题是,每次我通过每个元素上的集合 (MovieCollection) 的获取函数添加新对象时,都会添加某种事件触发器,并初始化一个新的 MovieList 集合在获取所有项目后传递到的对象。 这是我
我正在开发一个基本的调查应用程序来学习 Marionette/很好地掌握嵌套模型(答案 > 问题 > 调查)。我正在使用 backbone-relational 来完成这个。 在调查 View 中,我
我需要向 listView.template 传递一个值,以便了解有关 collection.length 的模板。 我认为一种选择是重新定义 serializeData 方法以便以这种方式传递参数。
我需要向 listView.template 传递一个值,以便了解有关 collection.length 的模板。 我认为一种选择是重新定义 serializeData 方法以便以这种方式传递参数。
简而言之,这就是我所需要的。 我需要显示一个下拉列表/组合框,其中的子项可以来自许多集合。所有这些集合的模型都有一个文本属性,我将在下拉列表中显示该属性。 我想知道是否可以从许多集合中创建下拉列表。
我希望我的合成自动呈现一个集合,它已经这样做了。不过,我也希望它能为我获取一个模型,并为该模型提供复合访问权限。 基本上,我正在尝试使用下面的一组客户呈现用户详细信息。 复合 View define(
我只需要渲染获取的集合的第一个元素。 ItemView = Backbone.Marionette.ItemView.extend({ template: "#item" }); Items
我正在尝试对 Marionette.CompositeView 中的集合进行排序。 我有一个看起来像这样的集合: [ {id: 1, name: 'bar'}, {id: 2, name:
我想用 Backbone.Marionette 创建一个 View 来呈现模型,然后维护 2 个或更多 CollectionView。基本上是一个具有多个 CollectionView 的 Compo
我想从 Marionette.ItemView 访问 app.vent。 也许可以选择将参数 (app.vent) 从 Marionette.CompositeView 传递到 Marionette.
我们必须显示以下 View : 类别(显示为表格),每个类别表格应该有产品(tr)。 我们如何使用 Marionette itemView 和 CompositeView 构建它? 我们需要产品成为一
我处于以下情况。 当 collection.length = 0 我想将一个参数传递给 listTemplate 以便在 listTemplate 中显示一条消息(没有模型存在!)。我怎样才能实现我的
我有一个具有父子关系的模型,我想在表格显示中显示每个父级,然后是子级,我将在其中稍微缩进子级。我已经快到了,但我似乎只能将子项附加到父行,而不是将它们添加到父行之后。 View.TreeView =
实际上,我想更改现有 Marionette CompositeView 中的整个集合,而不为该 View 创建新对象。 我无法找到用于此目的的方法我已阅读文档 here 最佳答案 您应该能够通过使用集
我不知道为什么这段代码不起作用。 阅读documentation , 应该调用 templateHelpers。 我的目标是将 this.collection.length 传递给模板。 有什么提示吗
我想显示集合中的前 100 个条目,或 101 到 200 之间的条目。 如果我创建一个 Marionette CompositeView 并指定 someRegion.show new MyComp
我现在坚持了。我收到以下错误: TypeError: listenTo is undefined return listenTo.call(this, evtSource, events, _.
我正在尝试设置一个简单的 Marionette 的 CompositeView。这就是我最终想要的: %select#target-id %option{:value => "#{@id}"} =
通过使用 jasmine,我正在尝试测试一个返回 Marionette.CompositeView 的 javascript 模块。 这是 javascript 模块 (2) 和 javascript
我想测试一个模块的行为(使用 Marionette )是否有效 (1)。 奇怪的是,js 模块 (1) 有效,但单元测试 (2) 使用 Jasmine失败。 有什么想法吗? (1) /*global
我是一名优秀的程序员,十分优秀!