Skip to content

Instantly share code, notes, and snippets.

@adbrowne
Created July 31, 2012 06:20
Show Gist options
  • Save adbrowne/3214211 to your computer and use it in GitHub Desktop.
Save adbrowne/3214211 to your computer and use it in GitHub Desktop.
Sample CoffeeScript ViewModel for KnockoutJS
class SampleViewModel
constructor: (values) ->
this.title = ko.observable()
this.description = ko.observable()
this.isPrivate = ko.observable()
this.status = ko.observable "Active"
this.showApproveReject = ko.computed => @isActive()
this.statusUrl = ko.observable()
this.privacyUrl = ko.observable()
_.extend(this, Backbone.Events)
this.isPrivate.subscribe (newValue) =>
this.trigger("privacyChanged", newValue)
isActive: ->
@status() == "Active"
approve: ->
@trigger("approved")
@status("Approved")
return false
reject: ->
@trigger("rejected")
@status("Rejected")
return false
window.app = window.app || {}
window.app.SampleViewModel = SampleViewModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment