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
- Host: GitHub
- URL: https://github.com/itsnickbarry/shallow-nested-model
- Owner: ItsNickBarry
- License: mit
- Created: 2016-05-12T07:46:15.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-12-29T20:41:06.000Z (over 9 years ago)
- Last Synced: 2024-12-27T06:26:41.938Z (over 1 year ago)
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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`.