Let's say I have a map:
map = new can.Map({foo: 'bar'})
and I want to bind the value of foo
in another map. I could do:
otherMap = new can.Map({fizzle: map.compute('foo')})
but this doesn't behave the way I would expect.
- I would expect
otherMap.attr('fizzle')
to returnbar
, but instead it returns a function. I have to callotherMap.attr('fizzle')()
instead. - I would expect to be able to change the value by calling
otherMap.attr('fizzle', 'moo')
, but this doesn't change the computed value. Instead, if I want to change the underlying value, I have to callotherMap.attr('fizzle')('moo')
.
Is there a way to create a map with computed values that behave like normal attributes?
Thanks!
I would recommend using the define plugin which makes it easy to create computed getters and setters without having to explicitly create computes. In your example like this:
Demo in this Fiddle.