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

The Archive Base Latest Questions

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

I have created a simple Todo application on JS Fiddle to learn Backbone.JS. I

  • 0

I have created a simple Todo application on JS Fiddle to learn Backbone.JS. I have a TodosModuleView that wraps a form and TodosView which renders the Collection of Todos

window.TodosModuleView = Backbone.View.extend({
    tagName: 'section',
    id: 'todoModule',
    events: {
        'keypress #frmTodo input[type=text]': 'addTodo'
    },
    initialize: function() {
        _.bindAll(this, 'render', 
                        'addTodo');
        this.collection.bind('reset', this.render);
        this.template = _.template($('#todosModuleTmpl').html());        },
    render: function() {
        console.log('rendering...');
        var todosView = new TodosView({ collection: this.collection });
        this.$el.html(this.template({}));
        this.$el.append(todosView.render().el);
        return this;
    },
    addTodo: function(e) {
        if (e.keyCode !== 13) 
            return;
        var todo = new Todo({ title: this.$('input[name=todo]').val() });
        this.collection.add(todo);
        console.log('added!');
        return false;
    }
});

When I add a todo, I can see it added to the collection, but it does not appear to trigger render(). Also since I am using a Local Storage store, I’d expect that my newly added Todos should be persisted and rendered on next refresh, but that does not appear to happen. Looking at the Chrome’s developer toolbar, I don’t see anything in Local Storage

UPDATE

1st Problem solved with @mashingan’s answer: use add instead of reset event. Now whats wrong with my Local Storage?

window.Todos = Backbone.Collection.extend({
    model: Todo,
    localStorage: new Backbone.LocalStorage('todos')
});

Could it be that variables are passed by value instead of reference as I’d expect? I have a TodosModuleView that uses TodosView to render the todo list, maybe I am doing it the wrong way?

  • 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-05T02:11:01+00:00Added an answer on June 5, 2026 at 2:11 am

    Your LocalStorage isn’t working because you’re not saving anything. This:

    var todo = new Todo({ title: this.$('input[name=todo]').val() });
    this.collection.add(todo);
    

    simply creates a new model and adds it to the collection, there is no hidden todo.save() call in there so the new Todo doesn’t get saved. You’d have to save it yourself:

    var todo = new Todo({ ... });
    todo.save();
    this.collection.add(todo);
    

    You could also save everything in the collection with:

    this.collection.invoke('save');
    

    That will call save on each model in the collection. This might make sense for LocalStorage but not so much sense if you’re persisting to a remote server.

    If you do this:

    var M = Backbone.Model.extend({});
    var C = Backbone.Collection.extend({
        model: M,
        localStorage: new Backbone.LocalStorage('pancakes')
    });
    var c = new C;
    c.add([
        { title: 'Fargo' },
        { title: 'Time Bandits' }
    ]);
    

    Then you won’t get anything in your pancakes database, but if you add c.invoke('save') at the end:

    var M = Backbone.Model.extend({});
    #...
    c.add([ ... ]);
    c.invoke('save');
    

    You will get a couple of good movies saved.

    Demo: http://jsfiddle.net/ambiguous/ZV86g/

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

Sidebar

Related Questions

I have created a simple win 32 application..in which it has a textbox and
I have created a simple Facebook application in PHP, that greets user by there
I have created a simple application and edited the index.erb file so that I
I have created the Simple Application of the sqlite database. In which i am
I have created a simple test form with FormBorderStyle = FixedToolWindow by default and
i have created a simple public ref class in the vc++ project, which is
I have created a simple WordPress plugin that automatically sets my new sites up
I have created a simple windows service that periodically checks a remote database via
I have created simple application in windows sharepoint services. Now I want to deploy
I have created a simple command line tool that outputs hello world. This is

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.