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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:10:09+00:00 2026-06-11T10:10:09+00:00

In the backbone.js ToDos example , The initialize method of the ToDo constructor sets

  • 0

In the backbone.js ToDos example, The initialize method of the ToDo constructor sets the title attribute to the default title.

Isn’t this unnecessary? I thought the point of defaults is that they get assigned automatically? Or am I missing something?

var Todo = Backbone.Model.extend({ 

    // Default attributes for the todo item.
    defaults: function() {
      return {
        title: "empty todo...",
        order: Todos.nextOrder(),
        done: false
      };
    },

    // Ensure that each todo created has `title`.
    initialize: function() {
      if (!this.get("title")) {
        this.set({"title": this.defaults().title});
      }
    },

    ///...

);}
  • 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-11T10:10:11+00:00Added an answer on June 11, 2026 at 10:10 am

    A default value will only be applied if no corresponding attribute is passed to the constructor. In this case, it’s probably to ensure that an item created with an empty string as a title gets displayed with something in it. Compare

    var Todo1 = Backbone.Model.extend({
    
        defaults: function() {
          return {
            title: "empty todo...",
            done: false
          };
        },
    
        initialize: function() {
          if (!this.get("title")) {
            this.set({"title": this.defaults().title});
          }
        }
    });
    var t1 = new Todo1({
        title: ""
    });
    

    with

    var Todo2 = Backbone.Model.extend({
    
        // Default attributes for the todo item.
        defaults: function() {
          return {
            title: "empty todo...",
            done: false
          };
        }
    });
    
    var t2 = new Todo2({
        title: ""
    });
    

    t1.get('title') will be empty todo… and t2.get('title') will be an empty string. Passing no argument to both constructors would indeed use the default values.

    And a Fiddle http://jsfiddle.net/nikoshr/CeEDg/

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

Sidebar

Related Questions

I am using very similar code to the Backbone ToDo example - http://documentcloud.github.com/backbone/docs/todos.html .
In the example Todos app for backbone.js, this takes place: clearCompleted: function() { _.each(Todos.done(),
I am new to Backbone and started by working through the Todos example. After
I've finally got my Rails Todo List app to display the Todos using Backbone's
This question is about the Todos Backbone.js sample, which is at: http://documentcloud.github.com/backbone/docs/todos.html The following
Experience level: newbie. The backbone.js Todos demo uses localStorage. This question is about how
In backbone.js under the inherits method, the authors does this: var ctor = function()
var homeView = Backbone.View.extend({ el: $(#main_container), initialize: function(){ _.bindAll(this, 'render'); }, render:function(){ $.get('/home', {},
Trying to create a todo example app to mess around with backbone. I cannot
I am studying Backbone and the todo example apps from http://todomvc.com/ I have noticed

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.