Skip to content

Instantly share code, notes, and snippets.

@d0minicw0ng
Created January 24, 2015 05:25
Show Gist options
  • Save d0minicw0ng/98293953048e59eff73f to your computer and use it in GitHub Desktop.
Save d0minicw0ng/98293953048e59eff73f to your computer and use it in GitHub Desktop.
Knockout Lazy ViewModel
class OPL.ViewModels.Lazy extends OPL.ViewModels.Base
###
lazyFlag is a string that holds the flag's name.
When the flag changes from false to true, it creates
the lazy objects and methods. When it changes from true
to false, it destroys the lazy objects and methods.
###
lazyFlag: "editMode"
constructor: ->
super()
@_subscribe @[lazyFlag], (shouldLoad) =>
if shouldLoad then @lazyLoad() else @destroyLazyObjects()
lazyLoad: ->
###
Load the observable values that the view model needs
when its `editMode` flag is set to true
###
@editedTitleContent = ko.observable ""
@editedDueBy = ko.observable ""
# Methods that are needed only when `editMode` is set to true
@updateTodoGroup = ->
# Do something
destroyLazyObjects: ->
###
Destroy the observable values and methods that the view model
doesn't need when its `editMode` flag is set to false
###
delete @editedTitleContent
delete @editedDueBy
delete @updateTodoGroup
@d0minicw0ng
Copy link
Author

dispose instead of delete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment