0

Loosely said, the various components in a MVVM pattern are

  • Model: this represents the data send by the server and sent back to the server. this contains no state related to display of the UI
  • ViewModel: this is constructed from one or models. this contains state meant for UI manipulation (is the button enabled or disabled). all logic meant for UI manipulation is stored here. this layer has no dependency on any UI framework (no jQuery calls)
  • View: this has tight coupling with the UI framework/underlying UI controls. One view observes one and only one view model. A view model can be observed by one or more views. The view is responsible for doing two-binding with the view model.
  • A presenter/coordinator: While not a part of the traditional implementation, in its absence the view model ends up with way too much responsibility. This guy helps coordinate making ajax calls (get/post), listening to events on the global event aggregator etc

Standalone Backbone has no concept of view models and data binding. In that scenario, the data returned by the server can be modelled as Backbone.Model objects. The bindings are done manually and a POJO can be used for view-model syncing.

If I wish to use Stickit for data binding, it appears that the view model needs to be an instance of Backbone.Model. Chiefly because the bindings work within the context of a Backbone.View and a Backbone.View expects a Backbone.Model object to be present as a property of the view. Also, a Backbone.Model raises change events and what not. I assume it will be difficult to observe a POJO. Again, this is my understanding from reading the Stickit docs. Please correct me if I am wrong.

A Backbone.Model has other methods on it that don't make sense from the point of view of a view model, like save, fetch etc. I was reading up on another mvvm library, Knockback. It can transform a Backbone.Model into a Knockout.js view model. Instead of passing in a full fledged Backbone.Model, it can also accept any POJO that has get/set methods and raises change events when the properties have changed.

Does Stickit have a similar contract wherein I can pass in a POJO that has get/set methods and raises change events? What is the recommended usage?

4

1 回答 1

0

Backbone.Stickit 的源代码中没有任何内容要求模型实际上是 Backbone.Model 的实例。所以,看起来 Stickit 只需要一些支持 Backbone.Model 提供的合约的对象——我认为你需要的只是 set()、get() 和 on() 的各种应用。

看看 Stickit 的测试套件。如果您编写了自己的模型 API 并通过了这些测试(通过在testScaffolding.js中用您自己的实现替换 Backbone.Model - 并假设测试是彻底的 - 那么您应该能够将该模型与 Stickit 一起使用。

编辑:我可能没有直接解决这个问题。Stickit 只要求您在 Backbone.View 中使用它,并且该视图具有一个model或其他由optionalModel您可以传递给stickit()函数的参数指定的对象,这些对象符合 Backbone.Model 提供的合同。

于 2014-04-15T21:58:08.593 回答