Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:59:01+00:00 2026-06-05T07:59:01+00:00

Say I have a Backbone application with a Posts collection. All posts belong to

  • 0

Say I have a Backbone application with a Posts collection. All posts belong to a Blog. Creating a new post requires me to know the Blog it belongs to: POST /blog/42/posts

Here is what I came up with so far — please tell me if there is a better solution:

Knowing that Backbone doesn’t expect me to model the relationships between my models, I simply turned the url attribute into a function to include the Blog ID:

class App.Collections.PostsCollection extends Backbone.Collection
  model: App.Models.Post
  url: -> "/blog/" + this.blogId + "/posts"

(Please excuse the CoffeeScript.) Now I need to make the blogId known to the posts collection. So I’m just tacking it on in the router initialize function:

class App.Routers.PostsRouter extends Backbone.Router
  initialize: (options) ->
    this.posts = new App.Collections.PostsCollection()
    this.posts.blogId = 42  # <----- in reality some meaningful expression ;-)
    this.posts.reset options.positions

That just can’t be right?!

Please enlighten me — how do you usually model these nested collections?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:59:03+00:00Added an answer on June 5, 2026 at 7:59 am

    This is one way to solve this problem. An alternative is to create a Blog model. Say you give the Blog model a posts() method. In this case you could use a similar method of attaching the blogId, but in the Blog Model. For example (also in Coffeescript, as above):

    class App.Models.Blog extends Backbone.Model
      posts: () =>
        @_posts ?= new App.Collections.PostsCollection({blog: this})
    

    and then in your PostsCollection you would have:

    class App.Collections.PostsCollections extends Backbone.Collection
      url: () => "/blog/#{@options.blog.id}/posts"
    

    allowing you to change your router to:

    class App.Routers.PostsRouter extends Backbone.Router
      initialize: (options) ->
        # Per your example, an ID of 42 is used below.
        # Ideally, you retrieve the Blog some other way.
        @blog = new App.Models.Blog({id: 42})
        @posts = @blog.posts()
    

    Modifying your structure like this might result in an extra model, but it makes your code much cleaner overall.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following Backbone Router: class App.Routers.ThingsRouter extends Backbone.Router initialize: -> new
I am new to backbone.js , and have a query. Say my user is
let's say I have : var Book = Backbone.Model.extend(); var Collection = Backbone.Collection.extend({ model:
I'm using codebrew\backbone-rails in a nested model example (say I have a collection of
I am creating a web application using backbone.js. I have two tabs. Tab 1
Let's say I have a collection in backbone and I want to be able
Let's say, I have a backbone.js model named Item that is stored in an
I have a question about the architecture of a backbone.js web app. Let's say
I've got a Backbone.Collection full of models; let's say that model is Car .
Let's say have a string... String myString = my*big*string*needs*parsing; All I want is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.