Created
January 24, 2015 05:25
-
-
Save d0minicw0ng/98293953048e59eff73f to your computer and use it in GitHub Desktop.
Knockout Lazy ViewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dispose
instead ofdelete