| CARVIEW |
Select Language
HTTP/2 302
date: Mon, 29 Dec 2025 14:17:51 GMT
content-type: text/html; charset=utf-8
content-length: 0
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
location: https://gist.github.com/jashkenas/1005723
cache-control: no-cache
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com github.githubassets.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com marketplace-screenshots.githubusercontent.com/ copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com github.githubassets.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
set-cookie: _gh_sess=odNZKQiHs6zrDDoedaGdrJFnh8RbquT82%2FaDsnojamsyJ4Em6G%2FC4GcL1vb3Ma5HnkVyDJMPI17QUkbouKQhT90R5Pxxa05cvz31UTIhmIOMLJQM%2FyPlGKWu9rnXSjVppLhR7AbvPr5Vf35c3dF2jv1cC69hpyu925YlRJlV6lwvMOZE4aa9LlACQxXEEP1rG7n2HGOYAz6XRr8Vg%2FuDJnOHxZDgt4CtZb2NQYYkkrYlRRiO2%2FqimmnBghy4Pkk0D%2BBnPebWbFwzfU26ATtquA%3D%3D--rhUiWem3G9XmM%2BCr--vcgZXTHk2haawPVntRdn7w%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.607931729.1767017871; Path=/; Domain=github.com; Expires=Tue, 29 Dec 2026 14:17:51 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Tue, 29 Dec 2026 14:17:51 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: A6EC:13ACF:6290DA4:762BE9B:69528D8F
HTTP/2 200
date: Mon, 29 Dec 2025 14:17:51 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"61fe6bf02eb51482eff5f11c9d075eda"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com github.githubassets.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com marketplace-screenshots.githubusercontent.com/ copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com github.githubassets.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
x-github-request-id: A6EC:13ACF:6290DF1:762BEF1:69528D8F
spine.coffee · GitHub
Show Gist options
Copy link
{{ message }}
Instantly share code, notes, and snippets.
-
Star
17
(17)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
-
Save jashkenas/1005723 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # In general, there are many places in the file where you can leave off the parens in function calls, if you like. | |
| unless typeof exports is "undefined" # `unless ... else` reads poorly in English. Better to stick to `if ... else`. | |
| Spine = exports | |
| else | |
| Spine = @Spine = {} | |
| Spine.version = "0.0.4" | |
| $ = Spine.$ = @jQuery || @Zepto || -> arguments[0] # In Coffee, `or` is preferred over `||`. | |
| makeArray = Spine.makeArray = (args) -> | |
| Array.prototype.slice.call(args, 0) # A shortcut for prototype accessors is: `::` `Array::slice` will do. | |
| isArray = Spine.isArray = (value) -> | |
| Object.prototype.toString.call(value) == "[object Array]" # Ditto. `Object::toString` | |
| Events = Spine.Events = | |
| bind: (ev, callback) -> | |
| evs = ev.split(" ") | |
| calls = @_callbacks || @_callbacks = {} | |
| for name in evs | |
| @_callbacks[name] ?= [] # `or=` here would be more efficient, if it matters. | |
| @_callbacks[name].push(callback) | |
| @ | |
| trigger: (args...) -> | |
| ev = args.shift() | |
| list = @_callbacks?[ev] | |
| return false unless list | |
| for callback in list | |
| if callback.apply(this, args) is false | |
| break | |
| true | |
| unbind: (ev, callback) -> | |
| unless ev | |
| @_callbacks = {} | |
| return @ | |
| list = @_callbacks?[ev] | |
| return @ unless list | |
| unless callback | |
| delete @_callbacks[ev] | |
| return @ | |
| for cb, i in list # You can use `when` to filter a comp. `for cb, i in list when cb is callback` | |
| if callback == cb | |
| list.splice(i, 1) | |
| break | |
| @ | |
| Log = Spine.Log = | |
| trace: true | |
| logPrefix: "(App)" | |
| log: (args...) -> | |
| return unless @trace | |
| return if typeof console == "undefined" # In Coffee, prefer `is` to `==`. | |
| if @logPrefix then args.unshift(@logPrefix) | |
| console.log.apply(console, args) | |
| @ | |
| # Classes (or prototypial inheritors) | |
| unless typeof Object.create is "function" # This faux Object.create is not API compatible with the real thing, so be careful. | |
| Object.create = (o) -> | |
| F = -> | |
| F.prototype = o | |
| new F() | |
| moduleKeywords = ["included", "extended"] | |
| Class = Spine.Class = | |
| inherited: -> | |
| created: -> | |
| prototype: | |
| initialize: -> | |
| init: -> | |
| create: (include, extend) -> | |
| object = Object.create(@) | |
| object.parent = @ | |
| object:: = object.fn = Object.create(@::) | |
| object.include(include) if include | |
| object.extend(extend) if extend | |
| object.created() | |
| @inherited(object) | |
| object | |
| init: -> | |
| instance = Object.create(@::) | |
| instance.parent = @ | |
| instance.initialize.apply(instance, arguments) | |
| instance.init.apply(instance, arguments) | |
| instance | |
| proxy: (func) -> | |
| => func.apply(this, arguments) | |
| proxyAll: (names...) -> | |
| @[name] = @proxy(@[name]) for name in names | |
| include: (obj) -> | |
| for key, value of obj # Again, you can use a `when` if you like. | |
| unless key in moduleKeywords | |
| @::[key] = value | |
| included = obj.included | |
| included.apply(this) if included | |
| @ | |
| extend: (obj) -> | |
| for key, value of obj | |
| unless key in moduleKeywords | |
| @[key] = value | |
| extended = obj.extended | |
| extended.apply(this) if extended | |
| @ | |
| Class::proxy = Class.proxy | |
| Class::proxyAll = Class.proxyAll | |
| Class.inst = Class.init | |
| Class.sub = Class.create | |
| Spine.guid = `function(){ | |
| return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
| var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); | |
| return v.toString(16); | |
| }).toUpperCase(); | |
| }` # This shouldn't need to be backtick'd. No? | |
| Model = Spine.Model = Class.create() | |
| Model.extend(Events) | |
| Model.extend | |
| setup: (name, atts) -> | |
| model = Model.sub() | |
| model.name = name if name | |
| model.attributes = atts if atts | |
| model | |
| created: (sub) -> | |
| @records = {} | |
| if @attributes then makeArray(@attributes) # Multi-line if/elses should probably use indentation. | |
| else @attributes = [] | |
| find: (id) -> | |
| record = @records[id] | |
| throw("Unknown record") unless record | |
| record.clone() | |
| exists: (id) -> | |
| try | |
| return @find(id) | |
| catch e | |
| return false | |
| refresh: (values) -> | |
| values = @fromJSON(values) | |
| @records = {} | |
| for record in values | |
| record.newRecord = false | |
| @records[record.id] = record | |
| @trigger("refresh") | |
| @ | |
| select: (callback) -> | |
| result = (record for id, record of @records when callback(record)) | |
| @cloneArray(result) | |
| findByAttribute: (name, value) -> | |
| for id, record of @records | |
| if record[name] == value | |
| return record.clone() | |
| null | |
| findAllByAttribute: (name, value) -> | |
| @select (item) -> | |
| item[name] == value | |
| each: (callback) -> | |
| for key, value of @records | |
| callback(value) | |
| all: -> | |
| @cloneArray(@recordsValues()) | |
| first: -> | |
| record = @recordsValues()[0] | |
| record?.clone() | |
| last: -> | |
| values = @recordsValues() | |
| record = values[values.length - 1] | |
| record?.clone() | |
| count: -> | |
| @recordsValues().length | |
| deleteAll: -> | |
| for key, value of @records | |
| delete @records[key] | |
| destroyAll: () -> # Functions that take no arguments require no parens before the arrow. | |
| for key, value of @records | |
| @records[key].destroy() | |
| update: (id, atts) -> | |
| @find(id).updateAttributes(atts) | |
| create: (atts) -> | |
| record = @init(atts) | |
| record.save() | |
| destroy: (id) -> | |
| @find(id).destroy() | |
| sync: (callback) -> | |
| @bind("change", callback) | |
| fetch: (callbackOrParams) -> | |
| if typeof(callbackOrParams) == "function" | |
| @bind("fetch", callbackOrParams) | |
| else | |
| @trigger("fetch", callbackOrParams) | |
| toJSON: -> | |
| @recordsValues() | |
| fromJSON: (objects) -> | |
| return unless objects | |
| if typeof objects == "string" | |
| objects = JSON.parse(objects) | |
| if isArray(objects) | |
| return (@init(value) for value in objects) | |
| else | |
| @init(objects) | |
| # Private | |
| recordsValues: -> | |
| result = [] | |
| for key, value of @records | |
| result.push(value) | |
| result | |
| cloneArray: (array) -> | |
| result = [] | |
| result.push value.clone() for value in array | |
| result | |
| Model.include | |
| model: true | |
| newRecord: true | |
| init: (atts) -> | |
| @load atts if atts | |
| @trigger("init", this) | |
| isNew: () -> | |
| @newRecord | |
| isValid: () -> | |
| not @validate() | |
| validate: -> | |
| load: (atts) -> | |
| for key, value of atts | |
| @[key] = value | |
| attributes: -> | |
| result = {} | |
| result[key] = @[key] for key in @parent.attributes | |
| result.id = @id | |
| result | |
| eql: (rec) -> | |
| rec && rec.id == @id && rec.parent == @parent | |
| save: -> | |
| error = @validate() | |
| if error | |
| @trigger("error", @, error) | |
| return false | |
| @trigger("beforeSave", @) | |
| if @newRecord then @create() else @update() | |
| @trigger("save", @) | |
| return @ | |
| updateAttribute: (name, value) -> | |
| @[name] = value | |
| @save() | |
| updateAttributes: (atts) -> | |
| @load(atts) | |
| @save() | |
| destroy: -> | |
| @trigger("beforeDestroy", @) | |
| delete @parent.records[@id] | |
| @destroyed = true | |
| @trigger("destroy", @) | |
| @trigger("change", @, "destroy") | |
| dup: -> | |
| result = @parent.init(@attributes()) | |
| result.newRecord = @newRecord | |
| result | |
| clone: -> | |
| Object.create(@) | |
| reload: -> | |
| return @ if @newRecord | |
| original = @parent.find(@id) | |
| @load(original.attributes()) | |
| return original | |
| toJSON: -> | |
| @attributes() | |
| exists: -> | |
| @id && @id of @parent.records # Prefer `and` to `&&`. | |
| # Private | |
| update: -> | |
| @trigger("beforeUpdate", @) | |
| records = @parent.records | |
| records[@id].load @attributes() | |
| clone = records[@id].clone() | |
| @trigger("update", clone) | |
| @trigger("change", clone, "update") | |
| create: -> | |
| @trigger("beforeCreate", @) | |
| @id = Spine.guid() unless @id | |
| @newRecord = false | |
| records = @parent.records | |
| records[@id] = @dup() | |
| clone = records[@id].clone() | |
| @trigger("create", clone) | |
| @trigger("change", clone, "create") | |
| bind: (events, callback) -> | |
| @parent.bind events, (record) => | |
| if record && @eql(record) | |
| callback.apply(@, arguments) | |
| trigger: -> | |
| @parent.trigger.apply(@parent, arguments) | |
| # Controllers | |
| eventSplitter = /^(\w+)\s*(.*)$/ | |
| Controller = Spine.Controller = Class.create | |
| tag: "div" | |
| initialize: (options) -> | |
| @options = options | |
| for key, value of @options | |
| @[key] = value | |
| @el = document.createElement(@tag) unless @el | |
| @el = $(@el) | |
| @events = @parent.events unless @events | |
| @elements = @parent.elements unless @elements | |
| @delegateEvents() if @events | |
| @refreshElements() if @elements | |
| @proxyAll.apply(@, @proxied) if @proxied | |
| $: (selector) -> | |
| $(selector, @el) | |
| delegateEvents: -> | |
| for key of @events | |
| methodName = @events[key] | |
| method = @proxy(@[methodName]) | |
| match = key.match(eventSplitter) | |
| eventName = match[1] | |
| selector = match[2] | |
| if selector == '' | |
| @el.bind(eventName, method) | |
| else | |
| @el.delegate(selector, eventName, method) | |
| refreshElements: -> | |
| for key, value of @elements | |
| @[value] = @$(key) | |
| delay: (func, timeout) -> | |
| setTimeout(@proxy(func), timeout || 0) | |
| Controller.include(Events) | |
| Controller.include(Log) | |
| Spine.App = Class.create() | |
| Spine.App.extend(Events) | |
| Controller.fn.App = Spine.App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.
I believe the reason that code is backticked is due to this Stack Overflow answer.