简体   繁体   中英

Rivets.js adapter publish vs setting value

Rivets.js proposes to use the adapter.read and adapter.publish functions to get and set properties of a model while defining binders. I have not found an actual benefit of using read / publish when compared to the standard get / set methodology.

Excerpt from documentation:

adapter.read(model, keypath)
adapter.publish(model, keypath, value)

The source code for read and publish from v0.6.10

read: function(obj, keypath) {
    return obj[keypath];
},
publish: function(obj, keypath, value) {
    return obj[keypath] = value;
}

I wonder if anyone knows about the benefits that read and publish may offer?

I finally figured this out. The answer is as simple as abstracting the get and set functionalities from the binder. This has no real benefit if using rivets as is with the one and only dot (.) binder which it ships with. But this approach comes in very handy when one defines custom adapters.

A good example, like in my case, is when using the rivets-backbone adapter. The model passed to the binder could be a plain old java object or a backbone model. Reading and writing of properties on the object vary based on its type. By using the publish and read functions, this logic gets abstracted from the binders implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM