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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:27:43+00:00 2026-06-04T19:27:43+00:00

This question is quite difficult to describe in words, so let me know if

  • 0

This question is quite difficult to describe in words, so let me know if it needs clarifying.
I’m very new to backbone.js, so it might be a silly question.

I want to use backbone to easily create graphs (highcharts library). I want to keep a massive json object with all common highcharts settings in my ‘defaults’ object in model. Then, I want to override some of the variables, for instance the one containing name of dom element the chart needs to be rendered in.

Here’s my code:

var app = {
    Views:{},
    Models:{},
    Collections:{},
    init:function(){
        new app.Views.PageView();
    }
};

$().ready(_.bind(app.init,app));

 app.Models.chart = Backbone.Model.extend({
    defaults:{
        chart:{
           renderTo:'charts',
           backgroundColor:'#e1e1e1',
           height:200,
           width:200,
           defaultSeriesType:'spline',
           spacingRight:40,
           animation:false
        },
        colors:['#000', '#AA4643'],
        title:{
           text:''
        },
//lots lots lots of other attributes defined here.

app.Views.ChartView = Backbone.View.extend({
    render : function() {
        var chartContainer = this.model.attributes.chart.renderTo;
        $('#charts').append("<div id="+chartContainer+"></div>");

        new Highcharts.Chart(this.model.toJSON());
        return this;
    }
});

var chartDetails = [{chart:{renderTo:'div1'}},{chart:{renderTo:'div2'}}];

app.Views.PageView = Backbone.View.extend({
    el: $("#charts"),

    initialize : function(){
        this.chartCollection = new app.Collections.Charts(chartDetails);
        this.render();
    },

    render: function(){
        var that = this;
        _.each(this.chartCollection.models, function(item){
            that.renderChart(item);
        })
    },

    renderChart : function (item) {
        var chartView = new app.Views.ChartView({
            model: item
        });
        chartView.render();
    }
});

So, the real question here is: when I define a new collection, I provide ‘chartDetails’ object, which overrides my ‘default’ specified in model. So, rather than update the defaults.charts.renderTo property, it replaces the full defaults.charts object. How do I make it keep everything what’s stored there (backgroundColor, etc), apart from the value I want to replace?

Thanks in advance
k

  • 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-04T19:27:44+00:00Added an answer on June 4, 2026 at 7:27 pm

    A very simple approach, if you had a flat representation of your model, would be something along these lines as you can define defaults as a function

    app.Models.Chart = Backbone.Model.extend(
    {
        // we're using constructor to set this.options 
        //  as defaults gets called before initialize
        constructor: function (attrs, options)
        {
            this.attrs = attrs;
            Backbone.Model.prototype.constructor.apply(this, arguments)
        },
        // defaults can be defined as a function, yeah!
        defaults: function ()
        {
            var defaults = {
                renderTo: 'charts',
                backgroundColor: '#e1e1e1',
                height: 200,
                width: 200,
                defaultSeriesType: 'spline',
                spacingRight: 40,
                animation: false
            };
            return _.extend({}, defaults, this.attrs);
        }
    });
    

    I prefer this implementation above an implementation like this because you’re not tying your bootstrap together in one go, this might actually trigger unwanted events:

    app.Models.Chart = Backbone.Model.extend(
    {
        initialize: function (attrs, options)
        {
            // this might trigger events, even if you make it {silent: true}
            // and is why I prefer the above implementation instead
            this.set(attrs); 
        },
        defaults: {
            renderTo: 'charts',
            backgroundColor: '#e1e1e1',
            height: 200,
            width: 200,
            defaultSeriesType: 'spline',
            spacingRight: 40,
            animation: false
        };
    });
    

    but since you have a deeply nested instead of a flat representation of your model, you will have to do some additional magic to only replace individual key/value pairs inside the nested object, as you would overwrite the whole object if you decide to extend it as in above example.

    Thankfully jQuery’s extend method, as opposed to underscore.js’s extend method provides us with a deep parameter so that you can do this:

    app.Models.Chart = Backbone.Model.extend(
    {
        // we're using constructor to set this.options 
        //  as defaults gets called before initialize
        constructor: function (attrs, options)
        {
            this.attrs = attrs;
            Backbone.Model.prototype.constructor.apply(this, arguments)
        },
        // defaults can be defined as a function, yeah!
        defaults: function ()
        {
            var defaults = {
                renderTo: 'charts',
                backgroundColor: '#e1e1e1',
                height: 200,
                width: 200,
                defaultSeriesType: 'spline',
                spacingRight: 40,
                animation: false
            };
            return $.extend(true, {}, defaults, this.attrs); // thank you jQuery!
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question might sound quite silly, but I somehow found myself stuck
I know this question is quite basic but I am new to JQuery and
This is a very basic question...quite embarassing, but here goes: I have a Stopwatch
I know this question has been rised quite a lot of times, but then
It is very difficult for me to explain this particular problem/question so please bear
I'm finding this to be a difficult question to put into words, hence the
I am rather new at java, and I know this cant be too difficult,
I've been looking for the answer to this question but it seems quite difficult
This question might sound fool, because I know there are bunch of frameworks that
This question was asked quite some time ago, and while it covers possible solutions

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.