An open API service indexing awesome lists of open source software.

https://github.com/itsnickbarry/shallow-nested-model

Backbone models to match Rails shallow nested routes
https://github.com/itsnickbarry/shallow-nested-model

Last synced: 8 months ago
JSON representation

Backbone models to match Rails shallow nested routes

Awesome Lists containing this project

README

          

# Shallow Nested Model
Use shallow nested models to allow Backbone to work with Rails shallow nested routes.

A shallow nested model created through a collection will only be posted to its own `url` if the collection's `url` does not exist. A preexisting model will use its own `url`, which is sufficient to uniquely identify it.

## Example

```ruby
resources :users, shallow: true do
resources :posts
end
```

```bash
$ rake routes
user_posts POST /users/:user_id/posts(.:format) posts#create
post GET /posts/:id(.:format) posts#show
DELETE /posts/:id(.:format) posts#destroy
```

```javascript
Post = Backbone.ShallowNestedModel.extend({
urlRoot: '/posts',
});

User = Backbone.Model.extend({
urlRoot: '/users',
initialize: function () {
this.posts = new UserPosts(this);
},
});

UserPosts = Backbone.Collection.extend({
model: Post,
url: '/posts',
});

var user = new User();
var post = user.posts.create({...});
post.fetch();
```

The new post will be posted to `/users/:user_id/posts`, but will be fetched from `/posts/:id`.