8

I am trying to understand the difference between Model and ViewModel in KO. I understand the conceptual difference, but to me it seems that all Models in KO will become or are candidates to become ViewModels. Ill explain:

Say you have a table with a row of seats, so in your main ViewModel you will initialise and load a collection of objects from a Seat Model into an observable array.

Now you want to hide display seats based on a certain property of seats...this is the point where your model becomes another viewmodel...

So is it right to say that in KO all we have is ViewModels?

4

1 回答 1

15

是的。本质上,任何带有 ako.observable的东西都在创建一个 ViewModel。您可以创建一个简单的 javascript 模型,只能表示为 JSON,并将其包装,但 Knockout 的 MVVM 模式中的“模型”通常只存在于server上。您从服务器获取模型数据,然后将模型数据发送回服务器(请记住,您不发送observables,只是发送它们的数据)

Knockout 并不真正关心模型的来源,因为它的主要作用是处理声明性视图 (HTML) 和 ViewModel(及其 View-aware observables)之间的数据绑定。

MVVM 模式由 Knockout 之外的技术完成,因为它是一个仅限客户端的框架。您可以说它只是该模式的 VVM 部分,但这令人困惑。

于 2013-01-25T06:24:42.327 回答