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 5971711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:33:16+00:00 2026-05-22T20:33:16+00:00

I’m trying to keep track of the state in this app using Backbone.js: I

  • 0

I’m trying to keep track of the state in this app using Backbone.js:

enter image description here

I have a “ChartAppModel” with a set of defaults:

ChartAppModel = Backbone.Model.extend({

defaults: { 
    countries : [], 
    selectedCountries : [],
    year : 1970,
},

initialize: function() { 
    loadAbunchOfData();
    checkStartState();
}

});

If given a start fragment, this default state should however be overwritten:

var startState = $.deparam.fragment(); //using Ben Alman's BBQ plugin
this.set({selectedCountries: startState.s, year: startState.y});

Now, for example the SidebarView is ready to be updated:

ChartAppViewSidebar = Backbone.View.extend({

initialize: function(){
      this.model.bind("change:selectedCountries", this.render);
},

render : function(){
      [... render new sidebar with check boxes ...]
},

Problem is I also have an event handler on the sidebar that updates the model:

events: {
"change input[name=country]": "menuChangeHandler",
},

menuChangeHandler : function(){
      [... set selectedCountries on model ...]
},

So there will be a feedback loop …
And then, I’d also like a way of pushing a new state – so I listen to model changes:

ChartAppModel = Backbone.Model.extend({

initialize: function() { 
    this.bind("change", this.setState);
}

});

… and relatively soon this state-manager will collapse …

Questions:

1) How do I init my views (for example “which checkboxes should be checked”) based on the fragment? (any hints on best practices for state / start state that is not a typical “route” are appreciated)

2) How can I avoid my views setting an attribute on the model which they themselves listen for?

3) How can I push a new state based on a part of the model?

Bonus 🙂

4) How would you lay out the code for the app described?

Thanks!

  • 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-05-22T20:33:16+00:00Added an answer on May 22, 2026 at 8:33 pm

    That is one well defined question!

    There is a question over what is a model. I believe there is a definition floating around as to what constitutes a model in the backbone world, and I’m not sure your strategy is in keeping with that definition. Also you are storing the state in both the url, and the model. You can just store the state in the url, as I will explain.

    If I was doing this, there would be 2 views. One for your app controls, and nested inside that one for your graph: GraphView, and AppView. The model will be the data your going to plot, not the state of the interface.

    Use a controller to kick start the app view and also to process any interface state defined in the url.

    There is a question about levers of state in Backbone. Traditional web applications used a link/url as the primary lever of state but all that is now changing. Here is one possible strategy:

    Checkbox Change Event -> Update location fragment -> trigger route in controller -> update the view
    Slider Change Event -> Update location fragment -> trigger route in controller -> update the view
    

    The great thing about such a strategy is that it takes care of the case where urls are passed around or bookmarked

    Url specified in address bar -> trigger route in controller -> update the view
    

    I’ll take a stab at a pseudo code example. For this, I will make some assumptions on the data:
    The data is the dog population over time (with a granularity of year), where the slider should have a lower and upper bound, and there volume data is too large to load it all to the client at once.

    First let’s look at the Model to represent the statistical data. For each point on the graph we need something like { population: 27000, year: 2003 }
    Lets represent this as

    DogStatModel extends Backbone.Model ->
    

    and a collection of this data will be

    DogStatCollection extends Backbone.Collection ->
        model: DogStatModel
        query: null // query sent to server to limit results
        url: function() {
            return "/dogStats?"+this.query
        }
    

    Now lets look at the controller. In this strategy I propose, the controller lives up to its name.

    AppController extends Backbone.Controller ->
        dogStatCollection: null,
        appView: null,
    
        routes: {
             "/:query" : "showChart"
        },
    
        chart: function(query){
            // 2dani, you described a nice way in your question
            // but will be more complicated if selections are not mutually exclusive
            // countries you could have as countries=sweden;france&fullscreen=true
            queryMap = parse(query) //  
            if (!this.dogStatCollection) dogStatCollection = new DogStatCollection
            dogStatCollection.query = queryMap.serverQuery
            if (!this.appView) {
               appView = new AppView()
               appView.collection = dogStatCollection
            }
            appView.fullScreen = queryMap.fullScreen
            dogStatCollection.fetch(success:function(){
              appView.render()
            })            
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.