knockout.viewmodel icon indicating copy to clipboard operation
knockout.viewmodel copied to clipboard

Custom mappings replace observables instead of updating

Open CuinnWylie opened this issue 12 years ago • 2 comments

Hi,

When implementing the custom mapping option and returning an observable the observable is replaced on update instead of actually being updated. ie. If I implement a custom mapping for DOB that converts to a date in a specific way then on update it recreates the observable instead of updating and breaks all bindings on the page.

I sorted this out for the time being by modifying the recursiveUpdate function as below. I'm sure that the way I've done it is not the best, but it works for me and I've put it here just so you can see what I've done.

Original

 unwrapped[p] = childMap(modelObj[p]);

Modified

                    if (isObservable(unwrapped[p])) {
                        if (isObservable(childMap(modelObj[p]))) {
                            unwrapped[p](childMap(modelObj[p])());
                        } else {
                            unwrapped[p](childMap(modelObj[p]));
                        }
                    } else {
                        unwrapped[p] = childMap(modelObj[p]);
                    }

Thanks.

CuinnWylie avatar Jul 15 '13 02:07 CuinnWylie

I prefer whatever conversion that's necessary be handled by a computed.

I use momentjs to covert my dates, so I see how this could be a problem if I were storing that conversion back to the model. If you're storing that conversion back to the model, aren't the corresponding "modified" events being fired?

I realize your DOB example is just that, an example. But essentially, you're modifying the data. Adding more checks on whether the data is observable ads overhead I personally haven't found necessary. On Jul 14, 2013 7:06 PM, "Cuinn Wylie" [email protected] wrote:

Hi,

When implementing the custom mapping option and returning an observable the observable is replaced on update instead of actually being updated. ie. If I implement a custom mapping for DOB that converts to a date in a specific way then on update it recreates the observable instead of updating and breaks all bindings on the page.

I sorted this out for the time being by modifying the recursiveUpdate function as below. I'm sure that the way I've done it is not the best, but it works for me and I've put it here just so you can see what I've done.

Original

unwrapped[p] = childMap(modelObj[p]);

Modified

                if (isObservable(unwrapped[p])) {
                    if (isObservable(childMap(modelObj[p]))) {
                        unwrapped[p](childMap(modelObj[p])());
                    } else {
                        unwrapped[p](childMap(modelObj[p]));
                    }
                } else {
                    unwrapped[p] = childMap(modelObj[p]);
                }

Thanks.

— Reply to this email directly or view it on GitHubhttps://github.com/coderenaissance/knockout.viewmodel/issues/38 .

spring1975 avatar Jul 15 '13 03:07 spring1975

Thanks, will look into it.

coderenaissance avatar Jul 15 '13 14:07 coderenaissance