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

  • Home
  • SEARCH
  • 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 8625853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:52:44+00:00 2026-06-12T07:52:44+00:00

I was trying to dynamically change the url inside the router but couldn’t manage

  • 0

I was trying to dynamically change the url inside the router but couldn’t manage to do it, it keeps returning to the base Collection URL. Here i posted the code with the 3 different collections which apart from pointing to three different urls they do exactly the same.
I have only one model and three collections that depend on that model and they even render the same view. How can i dynamically change the url so i can create only one Collection and one Model? Is it best pracitce for a case like this?

    // MODELS & COLLECTIONS 
    window.Post = Backbone.Model.extend({
            urlRoot:  function() {
                    return 'http://localhost:5000/json/guides/:id'
            }
    })

    App.Collections.RecentPosts = Backbone.Collection.extend({
            model: Post,
            url:'http://localhost:5000/json/posts/recent',
    })
    App.Collections.PopularPosts = Backbone.Collection.extend({
            model: Post,
            url:'http://localhost:5000/json/posts/popular',
    })
    App.Collections.FeaturedPosts = Backbone.Collection.extend({
            model: Post,
            url:'http://localhost:5000/json/posts/featured',
    })

    // CONTROLLER
    App.Controllers.Documents = Backbone.Router.extend({
            routes:{
            "recent"                : "recent",
            "popular"         : "popular",
            "featured"          : "featured",
            },

            recent: function(){
                //.... same as featured ?
            },
            popular: function(){
                //.... same as featured ?
            },
            featured: function(){
                    $("#browser").empty();
                    var collection = new App.Collections.Posts();
                    collection.fetch({
                                success: function(col,posts){
                                    new App.Views.GuideView({collection: posts});
                                },
                                error: function(error){
                                    console.log(error)
                                }
                        })
            }
    });
  • 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-12T07:52:45+00:00Added an answer on June 12, 2026 at 7:52 am

    There are numerous different ways of doing this. Here’s what’s probably going to be ‘best practice’.

    App.Controllers.Documents = Backbone.Router.extend({
            routes:{
            "recent"                : "recent",
            "popular"         : "popular",
            "featured"          : "featured",
            },
    
            initialize: function () {
                this.collection = new App.Collections.Posts();
            },
    
            _showPage: function (config) {
                $("#browser").empty();
    
                this.collection.fetch({
                    url: config.url,
                    success: function(col,posts){
                        new App.Views.GuideView({collection: posts});
                    },
                    error: function(error){
                        console.log(error)
                    }
                });
            },
    
            recent: function(){
                this._showPage({url:'http://localhost:5000/json/posts/recent'});
            },
            popular: function(){
                this._showPage({url:'http://localhost:5000/json/posts/popular'});
            },
            featured: function(){
               this._showPage({url:'http://localhost:5000/json/posts/featured'});
            }
    });
    

    Since I really don’t know how complicated your page is going to get, this is probably the best I can do without more information. But, the idea is that “this.collection” is set on the routers initialization.. so you can keep reusing it. The _showPage method does whatever basic tasks you need done to show the page, and the methods called by the routes use it to do whatever basic stuff needs done before going into detail. The url passed into the config would simply tell the collection where to get its information from – I’m assuming that all of your data has the same format and ‘is the same thing’.. just different filters.

    You can probably do a similar thing with App.Views.GuideView:

    App.Controllers.Documents = Backbone.Router.extend({
            routes:{
            "recent"                : "recent",
            "popular"         : "popular",
            "featured"          : "featured",
            },
    
            initialize: function () {
                this.collection = new App.Collections.Posts();
                this.view = new App.Views.GuideView({collection: this.collection});
            },
    
            _showPage: function (config) {
                $("#browser").empty();
    
                this.collection.fetch({
                    url: config.url,
                    success: _.bind(function(col,posts){
                        this.view.render();
                    }, this),
                    error: function(error){
                        console.log(error)
                    }
                });
            },
    
            recent: function(){
                this._showPage({url:'http://localhost:5000/json/posts/recent'});
            },
            popular: function(){
                this._showPage({url:'http://localhost:5000/json/posts/popular'});
            },
            featured: function(){
               this._showPage({url:'http://localhost:5000/json/posts/featured'});
            }
    });
    

    The ‘render’ would just rebuild the view, and since you’ve already got the collection referenced in the view as “this.options.collection” (or you could add an ‘initialize’ to the view and set this.collection to be this.options.collection). When the collection gets updated, all of that information is by reference in the view.. so no need to reset it.

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

Sidebar

Related Questions

I am trying to change the button size dynamically, but after using setWidth() and
I am trying to change the text color of a button(api: Button) dynamically, but
I'm trying to dynamically change the background image of a div inside of a
I'm trying to dynamically change the text of the button. But the UI of
I'm trying to dynamically change a backgroundcolor in a part of a listview, I
I am trying to change the checked attribute of dynamically created html radio button
I am trying to dynamically change an element's onClick event and I have something
I'm having a problem while trying to dynamically change a table cell's styling class
Hi i'm trying to dynamically change the dimensions of my parallel set chart. http://www.jasondavies.com/parallel-sets/
I'm trying to dynamically change the height of a cell than contains a div.

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.