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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:04:36+00:00 2026-06-06T13:04:36+00:00

I am putting together a backbone example in which models are created edited and

  • 0

I am putting together a backbone example in which models are created edited and deleted. I am able to save new models and edits to local storage, but am having a problem getting localstorage to properly display on refresh. It seems to be loading as a single object, and therefore gives me one model regardless of how many were added.

 var Thing = Backbone.Model.extend({
     defaults: {
         title: 'blank'
     }
 });
 var ThingView = Backbone.View.extend({
     template: _.template('<b><button id="remove">X</button> <b><button id="edit">Edit</button> <%= title %></b>'),
     editTemplate: _.template('<input class="name" value="<%= name %>" /><button id="save">Save</button>'),
     events: {
         "click #remove": "deleteItem",
         "click #edit": "editItem",
         "click #save": "saveItem",
     },
     deleteItem: function () {
         console.log('deleted');
         this.model.destroy();
         this.remove();
     },
     editItem: function () {
         console.log('editing');
         this.$el.html(this.editTemplate(this.model.toJSON()));
     },
     saveItem: function () {
         console.log('saved');
         editTitle = $('input.name').val();
         console.log(editTitle);
         this.model.save({
             title: editTitle
         });
         this.$el.html(this.template(this.model.toJSON()));
     },
     render: function () {
         var attributes = this.model.toJSON();
         this.$el.append(this.template(attributes));
         return this;
     }
 });
 var ThingsList = Backbone.Collection.extend({
     model: Thing,
     localStorage: new Store("store-name"),
 });
 var storeVar = localStorage.getItem("store-name");
 console.log(storeVar);
 var thingsList = new ThingsList;
 thingsList.reset(storeVar);
 var ThingsListView = Backbone.View.extend({
     el: $('body'),
     events: {
         'click #add': 'insertItem',
     },
     initialize: function () {
         this.render();
         this.collection.on("add", this.renderThing, this);
     },
     insertItem: function (e) {
         newTitle = $('input').val();
         newThing = new Thing({
             title: newTitle
         });
         this.collection.add(newThing);
         newThing.save();
         console.log(this.collection.length);
     },
     render: function () {
         _.each(this.collection.models, function (items) {
             this.renderThing(items);
         }, this);
     },
     renderThing: function (items) {
         var thingView = new ThingView({
             model: items
         });
         this.$el.append(thingView.render().el);
     }
 });
 var thingsListView = new ThingsListView({
     collection: thingsList
 });
  • 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-06T13:04:37+00:00Added an answer on June 6, 2026 at 1:04 pm
    var Thing = Backbone.Model.extend({
         defaults: {
             title: 'blank'
         }
     });
     var ThingView = Backbone.View.extend({
         template: _.template('<b><button id="remove">X</button> <b><button id="edit">Edit</button> <%= title %></b>'),
         editTemplate: _.template('<input class="name" value="<%= name %>" /><button id="save">Save</button>'),
         events: {
             "click #remove": "deleteItem",
             "click #edit": "editItem",
             "click #save": "saveItem",
         },
         deleteItem: function () {
             console.log('deleted');
             this.model.destroy();
             this.remove();
         },
         editItem: function () {
             console.log('editing');
             this.$el.html(this.editTemplate(this.model.toJSON()));
         },
         saveItem: function () {
             console.log('saved');
             editTitle = $('input.name').val();
             console.log(editTitle);
             this.model.save({
                 title: editTitle
             });
             this.$el.html(this.template(this.model.toJSON()));
         },
         render: function () {
             var attributes = this.model.toJSON();
             this.$el.append(this.template(attributes));
             return this;
         }
     });
     var ThingsList = Backbone.Collection.extend({
         model: Thing,
         localStorage: new Store("store-name"),
     });
     var storeVar = localStorage["store-name-7ee7d1e3-bbb7-b3e4-1fe8-124f76c2b64d"];
     console.log(storeVar);
     var thingsList = new ThingsList;
     //thingsList.reset(storeVar);
     var ThingsListView = Backbone.View.extend({
         el: $('body'),
         events: {
             'click #add': 'insertItem',
         },
         initialize: function () {
        thingsList.fetch();
        thingsList.toJSON();
             this.render();
             this.collection.on("add", this.renderThing, this);
         },
         insertItem: function (e) {
             newTitle = $('input').val();
             newThing = new Thing({
                 title: newTitle
             });
             this.collection.add(newThing);
             newThing.save();
             console.log(this.collection.length);
         },
         render: function () {
             _.each(this.collection.models, function (items) {
                 this.renderThing(items);
             }, this);
         },
         renderThing: function (items) {
             var thingView = new ThingView({
                 model: items
             });
             this.$el.append(thingView.render().el);
         }
     });
     var thingsListView = new ThingsListView({
         collection: thingsList
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help putting together this query in Django. I've simplified the example
little background: currently putting together a website that is selling products, many of which
I'm putting together a portfolio website which includes a number of images, some of
I'm currently putting together (on old fashioned paper) a layout for my new database,
I am putting together an old post here with this new one. My main
I am currently putting together a rails-based web application which will only serve and
I'm currently putting together some changes in our data model which include changing a
I'm putting together a demo web-app. I've created a certificate signed by my own
I'm putting together a JavaScript object called ListBox which takes a two-dimensional array, and
I am putting together a new dev machine, Mac. My old dev machine runs

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.