kmalakoff/knockback — Issue #12: Should we move some of the API to kb.utils?
Author: kmalakoff
Created: 2011-11-06 08:12:15+00:00
State: open
Labels: idea

Currently, there are a few utility functions:

1) For an instance of an class (from within the class)
- Knockback.wrappedObservable = (instance) ->

2) For an instance of a view model (in general)
- Knockback.viewModelDestroyObservables = Knockback.vmDestroy = (view model) ->

3) For an instance of a view model (for a model's view model created by a CollectionSync)
- Knockback.viewModelGetModel = Knockback.vmModel = (view_model) ->
- Knockback.viewModelGetElement = Knockback.vmElement = (view_model) ->

Where should they go?

1) Knockback.wrappedObservable can't be an function on the kb.Observable and kn.LocalizedObservable classes because it can also be called outside the class if you use this set up (instead of derivation):

class ShortDateLocalizer extends kb.LocalizedObservable
constructor: (value, options={}, view_model) ->
super(value, _.extend(options, {
read: (value) ->
return Globalize.format(date, Globalize.cultures[kb.locale_manager.getLocale()].calendars.standard.patterns.d, kb.locale_manager.getLocale())
write: (localized_string, value, observable) =>
new_value = Globalize.parseDate(localized_string, Globalize.cultures[kb.locale_manager.getLocale()].calendars.standard.patterns.d, kb.locale_manager.getLocale())
return observable.forceRefresh() if not (new_value and _.isDate(new_value)) # reset if invalid
date.setTime(new_value.valueOf())
}), view_model)
return kb.wrappedObservable(this)

Should there be some indication that it is sort of an internal function for kb.Observable and kn.LocalizedObservable instances rather than a common part of the API? eg. kb.utils._instanceGetWrappedObservable = kb.utils._instanceWO

2) Maybe Knockback.vmDestroy should be kb.utils.vmDestroy = kb.utils.viewModelDestroy?

3) Maybe Knockback.vmModel and Knockback.vmElement should be kb.utils.entryViewModelModel = kb.utils.evmModel?

Let me know what you think is best bearing in mind consistency with the Knockout API. Also, let me know if I should bother to have long and abbreviated forms of the functions!