Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elcuervo/backbone-atlas
Backbone compatible json to models converter
https://github.com/elcuervo/backbone-atlas
Last synced: about 1 month ago
JSON representation
Backbone compatible json to models converter
- Host: GitHub
- URL: https://github.com/elcuervo/backbone-atlas
- Owner: elcuervo
- Created: 2011-03-31T23:55:27.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2011-04-12T19:01:06.000Z (over 13 years ago)
- Last Synced: 2024-11-28T23:28:12.575Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 1.44 MB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Atlas
=====The name is from this [atlas](http://en.wikipedia.org/wiki/Atlas_(anatomy%29) and in a clear reference to [Backbone](https://github.com/documentcloud/backbone)
![Atlas Backbone](http://upload.wikimedia.org/wikipedia/commons/5/54/Gray_111_-_Vertebral_column-coloured.png)
Description
-----------First of all this is written in Coffeescript but it also provides a Javascript compiled version.
Its main purpose is to give Backbone the ability to load nested models from json, also gives some sugar to access the .get(attribute) values.### Example
#### Creating classes
class @Comment extends Backbone.Atlas.Model
initialize: (attributes) ->
@has attributes,
comments: CommentList
author: Userclass @CommentList extends Backbone.Atlas.Collection
model: Commentclass @Post extends Backbone.Atlas.Model
initialize: (attributes) ->
@has attributes, comments: CommentList, created_by: Userclass @User extends Backbone.Atlas.Model
This can be translated to [Rails](https://github.com/rails/rails) ActiveRecord like this:
class Comment < ActiveRecord::Base
has_many :comments
has_one :author, class_name: "User"
endclass Post < ActiveRecord::Base
has_one :created_by, class_name: "User"
has_many :comments
endclass User < ActiveRecord::Base
end#### Creating and loading instances
As we are talking of javascript objects we need to load data in them, so:
post = new Post(
{
'title': "Test Title",
'body': "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat.",
'created_by': {
'id': 1
'name': "Zeus"
},
'comments': [
{
'id': 4,
'body': "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat.",
'comments': [
{
'id': 54,
'body': "Hello from Valhala",
'author': { 'id': 90, 'name': "Odin"}
}
],
'author': {
'id': 2,
'name': "Hermes",
}
},
{
'id': 5,
'body': "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat.",
'author': {
'id': 3,
'name': "Persefone",
}
}
]
}
);So using Backbone.Atlas as you will use Rails:
post.title
=> "Test Title"post.comments.first()
=> Comment()post.comments.last().id
=> 5post.comments.first().comments.first().author.name
=> "Odin"