Skip to content

Instantly share code, notes, and snippets.

@thelinuxlich
Created March 3, 2011 20:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thelinuxlich/853470 to your computer and use it in GitHub Desktop.
Save thelinuxlich/853470 to your computer and use it in GitHub Desktop.
Sample Knockout.js model written in Coffeescript
# Modelo base
class @Model
constructor: (defaults,urls) ->
@__defaults = if typeof defaults is "object" then defaults else {}
@__urls = if typeof urls is "object" then defaults else {}
@set(@__defaults)
get: (attr) ->
ko.unwrapObservable @[attr]
set: (args) ->
for own i,item of args
if ko.isObservable @[i]
@[i](if item.match(/^&[^\s]*;$/) then item.unescapeHtml() else item)
else if @[i] isnt undefined
@[i] = if item.match(/^&[^\s]*;$/) then item.unescapeHtml() else item
clear: ->
values = {}
for own i of @
switch(typeof @get(@[i]))
when "string" then values[i] = (if @__defaults[i] isnt undefined then @__defaults[i] else "")
when "number" then values[i] = (if @__defaults[i] isnt undefined then @__defaults[i] else 0)
when "boolean" then values[i] = (if @__defaults[i] isnt undefined then @__defaults[i] else false)
when "object"
if toString.call() is "[object Array]"
values[i] = (if @__defaults[i] isnt undefined then @__defaults[i] else [])
@set(values)
toJSON: (options) ->
temp = @clone(options)
temp["__no_cache"] = new Date()
ko.utils.toJSON(temp)
toJS: (options) ->
temp = @clone(options)
temp["__no_cache"] = new Date()
ko.utils.toJS(temp)
clone: (args) ->
temp = {}
args = args or {}
options = $.extend({"__defaults": false,"__urls": false},args)
for own i of @
if options[i] is true or options[i] is undefined
temp[i] = @get(@[i])
temp
isNew: ->
value = @get("id")
value? && value isnt ""
validate: ->
true
save: ->
if @validate() is true
if @isNew() is true then @create(arguments) else @update(arguments)
else
callback({status: "ERROR",message: "Invalid object"})
__generate_request_parameters: ->
params = {}
callback = null
if typeof arguments[0] is "function"
callback = arguments[0]
else if typeof arguments[0] is "object"
params = arguments[0]
if typeof arguments[1] is "function"
callback = arguments[1]
[params,callback]
create: ->
[params,callback] = @__generate_request_parameters(arguments)
params = $.extend(params,@toJS())
RQ.add $.post @__url["post"], params, (data) ->
callback(data) if typeof callback is "function"
update: ->
[params,callback] = @__generate_request_parameters(arguments)
params = $.extend(params,@toJS())
RQ.add $.post @__url["put"], params, (data) ->
callback(data) if typeof callback is "function"
destroy: ->
[params,callback] = @__generate_request_parameters(arguments)
params = $.extend(params,{id: @get("id")})
RQ.add $.post @__url["delete"], params, (data) ->
callback(data) if typeof callback is "function"
fetchOne: ->
__no_cache = new Date()
[params,callback] = @__generate_request_parameters(arguments)
params = $.extend(params,{id: @get("id")})
RQ.add $.get @__url["get"]+"?foo="+__no_cache, {id: @get("id")}, (data) ->
callback(data) if typeof callback is "function"
fetchAll: ->
__no_cache = new Date()
[params,callback] = @__generate_request_parameters(arguments)
RQ.add $.get @__url["get"]+"?foo="+__no_cache, params, (data) ->
callback(data) if typeof callback is "function"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment