-
-
Save robinhowlett/3021226 to your computer and use it in GitHub Desktop.
Two small components for making Backbone play nicely with application/hal+json
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
window.HAL = {} | |
class HAL.Model extends Backbone.Model | |
constructor: (attrs) -> | |
super @parse(_.clone attrs) | |
parse: (attrs = {}) -> | |
@links = attrs._links || {} | |
delete attrs._links | |
@embedded = attrs._embedded || {} | |
delete attrs._embedded | |
return attrs | |
url: -> | |
@links?.self?.href || super() | |
class HAL.Collection extends Backbone.Collection | |
constructor: (obj, options) -> | |
super @parse(_.clone obj), options | |
parse: (obj = {}) -> | |
@links = obj._links || {} | |
delete obj._links | |
@embedded = obj._embedded || {} | |
delete obj._embedded | |
@attributes = obj || {} | |
if @itemRel? | |
items = @embedded[@itemRel] | |
else | |
items = @embedded.items | |
return items | |
reset: (obj, options) -> | |
# skip parsing if obj is an Array (i.e. reset items only) | |
obj = @parse(_.clone obj) if not _.isArray(obj) | |
return super(obj, options) | |
url: -> | |
@links?.self?.href |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment