2014-02-25 10:32:14 +00:00
|
|
|
---
|
|
|
|
title: Spine
|
2015-11-24 05:09:17 +00:00
|
|
|
category: JavaScript libraries
|
2023-03-13 12:02:33 +00:00
|
|
|
tags: [Outdated]
|
2014-02-25 10:32:14 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
## Models
|
|
|
|
|
2023-03-13 12:02:33 +00:00
|
|
|
### About
|
|
|
|
{: .-intro}
|
|
|
|
|
|
|
|
Spine is an MVC framework for JavaScript.
|
|
|
|
|
|
|
|
- <https://spine.github.io/>
|
|
|
|
|
|
|
|
### Models
|
|
|
|
|
2014-02-25 10:32:14 +00:00
|
|
|
class User extends Spine.Model
|
|
|
|
@configure "User", "name", "address"
|
|
|
|
|
|
|
|
fullName: ->
|
|
|
|
[@first, @last].join ' '
|
|
|
|
|
|
|
|
### JavaScript
|
|
|
|
|
|
|
|
// Subclassing
|
|
|
|
User = Spine.Model.sub()
|
|
|
|
|
|
|
|
### Class methods
|
|
|
|
|
|
|
|
.configure 'modelname', attributes...
|
|
|
|
|
|
|
|
# Inheritance
|
|
|
|
.include(Module)
|
|
|
|
.extend(Module)
|
|
|
|
|
|
|
|
.create(name: "John")
|
|
|
|
|
|
|
|
.count()
|
|
|
|
|
|
|
|
# Events
|
|
|
|
.on 'refresh change', (user) -> ...
|
|
|
|
.trigger 'event'
|
|
|
|
|
|
|
|
.change (user) -> ... # same as on('change')
|
|
|
|
.fetch (user) -> ... # same as on('fetch')
|
|
|
|
|
|
|
|
# JSON
|
|
|
|
.toJSON() # all records
|
|
|
|
.fromJSON(json) # from json string
|
|
|
|
.fromForm(el)
|
|
|
|
|
|
|
|
# Data
|
|
|
|
.records # Hash of instances
|
|
|
|
.attributes # array of attributes (from .configure)
|
|
|
|
|
|
|
|
# Convenience
|
|
|
|
.toString() #=> "User"
|
|
|
|
|
|
|
|
# Find by ID
|
|
|
|
.exists(1)
|
|
|
|
.find(1) # throws error
|
|
|
|
|
|
|
|
# Find by something
|
|
|
|
.select (u) u.name == 'bob'
|
|
|
|
.findByAttribute 'name', 'bob'
|
|
|
|
.findAllByAttribute 'name', 'bob'
|
|
|
|
|
|
|
|
.all()
|
|
|
|
.slice(6, 13) # cloned copies of instances
|
|
|
|
|
|
|
|
# Iterating
|
|
|
|
.each (user) ->
|
|
|
|
|
|
|
|
# Ends
|
|
|
|
.first()
|
|
|
|
.last()
|
|
|
|
|
|
|
|
# Deleting
|
|
|
|
.deleteAll()
|
|
|
|
.destroyAll()
|
|
|
|
.destroyAll({ ..options.. })
|
|
|
|
.destroy(2)
|
|
|
|
|
|
|
|
### Instance methods
|
|
|
|
|
|
|
|
user = new User();
|
|
|
|
|
|
|
|
user
|
|
|
|
.isNew()
|
|
|
|
.exists()
|
|
|
|
|
|
|
|
# Validation
|
|
|
|
.isValid()
|
|
|
|
.validate() # validate = (-> "Name required" unless @name)
|
|
|
|
|
|
|
|
.attributes() # hash of attr values
|
|
|
|
.eql(other) # equality check
|
|
|
|
|
|
|
|
# Update
|
|
|
|
.load(attrs)
|
|
|
|
.reload()
|
|
|
|
.fromForm(form)
|
|
|
|
.updateAttribute("name", "john")
|
|
|
|
.updateAttributes(name: "John")
|
|
|
|
|
|
|
|
# Event
|
|
|
|
.on 'event', -> ...
|
|
|
|
.trigger 'event'
|
|
|
|
|
|
|
|
# Retrieve
|
|
|
|
.toJSON()
|
|
|
|
|
|
|
|
# Persistence
|
|
|
|
.save()
|
|
|
|
|
|
|
|
.destroy()
|
|
|
|
.dup() # clone as unsaved
|
|
|
|
|
|
|
|
### Mixins
|
|
|
|
|
|
|
|
class User extends Spine.Model
|
|
|
|
@include MyModule
|
|
|
|
@extend MyModule
|
|
|
|
|
|
|
|
### Events
|
|
|
|
|
|
|
|
.on 'create'
|
|
|
|
.on 'update'
|
2017-10-22 11:08:21 +00:00
|
|
|
.on 'destroy'
|
2014-02-25 10:32:14 +00:00
|
|
|
|
|
|
|
.on 'save' # create / update
|
|
|
|
.on 'change' # create / update / destroy
|
|
|
|
|
|
|
|
.on 'refresh'
|
|
|
|
.on 'error' # validation error
|
|
|
|
|
|
|
|
## Ajax
|
|
|
|
|
2023-03-13 12:02:33 +00:00
|
|
|
### Ajax
|
|
|
|
|
2014-02-25 10:32:14 +00:00
|
|
|
class User extends Spine.Model
|
|
|
|
@extend Spine.Model.Ajax
|
|
|
|
|
|
|
|
@url: '/users'
|
|
|
|
@url: -> '/users'
|
|
|
|
scope: '2013'
|
|
|
|
|
|
|
|
### Using
|
|
|
|
|
|
|
|
User.fetch()
|
|
|
|
user = new User()
|
|
|
|
|
|
|
|
user.url() #=> "/users"
|
|
|
|
user.url('bands') #=> "/users/bands"
|
|
|
|
|
|
|
|
user.scope = 'admin'
|
|
|
|
user.url() #=> "/admin/users"
|
|
|
|
|
|
|
|
### Host
|
|
|
|
|
|
|
|
Spine.Model.host = 'http://endpoint'
|
|
|
|
|
|
|
|
### Ajax mapping
|
|
|
|
|
|
|
|
read → GET /collection
|
|
|
|
create → POST /collection (201 created)
|
|
|
|
update → PUT /collection/id
|
|
|
|
destroy → DELETE /collection/id
|
|
|
|
|
|
|
|
### Associations
|
|
|
|
|
|
|
|
class Photo extends Spine.Model
|
|
|
|
@belongsTo 'album', 'Album' # window['Album']
|
|
|
|
@belongsTo 'album', 'models/album' # via require.js
|
|
|
|
|
|
|
|
class Album
|
|
|
|
@hasMany 'photos', 'models/photo'
|
|
|
|
|
|
|
|
album.photos().all()
|
|
|
|
album.photos().create(name: "Vacation")
|
|
|
|
album.photos().find(id)
|
|
|
|
|
|
|
|
photo = Photo.create(album: album)
|
|
|
|
photo.album()
|
|
|
|
photo.album_id
|