View 或 ViewModel 是否负责将模型中的数据转换为 UI?
Is the View or the ViewModel responsible for transforming data from a Model to be presented in the UI?
我是 MVVM 模式的新手,这对我来说是一个普遍的问题,但我会使用一个具体的例子。
我有:
- 一个数据class
Place
包含纬度、经度、名称等
- 一个
MapsViewModel
,其中包含 MapsFragment
使用的地点列表 LiveData<List<Place>>
- 还有一个
MapsFragment
代表MVVM模式的View
。
根据我的理解 View
应该忘记 Model
,在这种情况下 MapsFragment
应该不知道 Place
模型。
我想用标记填充地图,但标记需要纬度和经度(以及名称等其他参数)所以我应该观察地点列表并将列表中的每个条目映射到它的标记。
我不确定是否应该将用于将地点映射到标记的代码放在 MapsViewModel
中,这样 MapsFragment
就可以调用 mapsViewModel.getMarkers()
并使用该标记列表来填充地图,
或者 MapsFragment
应该观察 mapsViewModel.getPlaces()
并根据响应将列表映射到标记,然后填充地图。
如果 MapsViewModel
应该负责映射位置,如果添加了新位置,我将如何观察 LiveData
上的变化?
如果 MapsFragment
应该负责映射,那么这不会破坏 Views
不应该知道模型的 MVVM 模式吗?
示例是用 Kotlin
编写的,但我没有用 Kotlin
标记问题,因为它不是特定于语言的。
在我目前的实施中,我正在观察这些地方 LiveData<List<Place>>
// MapsFragment
...
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mapsViewModel.getPlaces().observe(this, Observer { places ->
places?.forEach { place ->
var options = MarkerOptions()
options.position(LatLng(place.lat.toDouble(), place.lon.toDouble()))
mMap.addMarker(options)
}
})
}
...
// MapsViewModel
...
fun getPlaces(): LiveData<List<Place>> {
return Repositories.placesRepository.getPlaces()
}
...
P.S。
ListViews 和其他集合类型的一个附带问题:
每个单元格应该有自己的 ViewModel
还是整个 ListView 应该只有一个 ViewModel
?
在 MVVM 架构中,ViewModel
公开了与 View
相关的数据流。所以你的 View
绝对应该观察 Place
.
的列表
If the MapsFragment is supposed to be responsible for mapping then
doesn't that break the MVVM pattern where Views should not know about
the model?
我们很好,只要View
不是直接interacting/changing数据层(Model
)并且仅从ViewModel
获取所有可显示信息。
一般经验法则是,
ViewModel
公开的数据流应该足够简单,以便 View
.
直接使用
希望这能回答您的问题。
我是 MVVM 模式的新手,这对我来说是一个普遍的问题,但我会使用一个具体的例子。
我有:
- 一个数据class
Place
包含纬度、经度、名称等 - 一个
MapsViewModel
,其中包含MapsFragment
使用的地点列表 - 还有一个
MapsFragment
代表MVVM模式的View
。
LiveData<List<Place>>
根据我的理解 View
应该忘记 Model
,在这种情况下 MapsFragment
应该不知道 Place
模型。
我想用标记填充地图,但标记需要纬度和经度(以及名称等其他参数)所以我应该观察地点列表并将列表中的每个条目映射到它的标记。
我不确定是否应该将用于将地点映射到标记的代码放在 MapsViewModel
中,这样 MapsFragment
就可以调用 mapsViewModel.getMarkers()
并使用该标记列表来填充地图,
或者 MapsFragment
应该观察 mapsViewModel.getPlaces()
并根据响应将列表映射到标记,然后填充地图。
如果 MapsViewModel
应该负责映射位置,如果添加了新位置,我将如何观察 LiveData
上的变化?
如果 MapsFragment
应该负责映射,那么这不会破坏 Views
不应该知道模型的 MVVM 模式吗?
示例是用 Kotlin
编写的,但我没有用 Kotlin
标记问题,因为它不是特定于语言的。
在我目前的实施中,我正在观察这些地方 LiveData<List<Place>>
// MapsFragment
...
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mapsViewModel.getPlaces().observe(this, Observer { places ->
places?.forEach { place ->
var options = MarkerOptions()
options.position(LatLng(place.lat.toDouble(), place.lon.toDouble()))
mMap.addMarker(options)
}
})
}
...
// MapsViewModel
...
fun getPlaces(): LiveData<List<Place>> {
return Repositories.placesRepository.getPlaces()
}
...
P.S。
ListViews 和其他集合类型的一个附带问题:
每个单元格应该有自己的 ViewModel
还是整个 ListView 应该只有一个 ViewModel
?
在 MVVM 架构中,ViewModel
公开了与 View
相关的数据流。所以你的 View
绝对应该观察 Place
.
If the MapsFragment is supposed to be responsible for mapping then doesn't that break the MVVM pattern where Views should not know about the model?
我们很好,只要View
不是直接interacting/changing数据层(Model
)并且仅从ViewModel
获取所有可显示信息。
一般经验法则是,
ViewModel
公开的数据流应该足够简单,以便 View
.
希望这能回答您的问题。