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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:41:07+00:00 2026-06-17T15:41:07+00:00

I’m learning backbone.js and have a problem following this tutorial . I have a

  • 0

I’m learning backbone.js and have a problem following this tutorial. I have a Collection of Eras called History. When trying app.History.create({ from: 0, until: 1, stash: {from: null, until: null}, _enabled: true}) I get the error in the title.

Here’s my code:

The Model

var app = app || {};

app.Era = Backbone.Model.extend({

    defaults: {
        from: Number.NEGATIVE_INFINITY,
        until: Number.POSITIVE_INFINITY,
        stash: {
            from: null,
            until: null
        },
        _enabled: true
    },

    toggle: function(){
        if(this.get('_enabled')){
            this.disable();
        }else{
            this.enable();
        }

        this.save();
    },

    enable: function(){
        this.from = this.stash.from;
        this.until = this.stash.until;

        this.stash.from = null; // strictly speaking unnecssary
        this.stash.until = null;

        this._enabled = true;
    },

    disable: function(){
        this.stash.from = this.from;
        this.stash.until = this.until;

        this.from = null;
        this.until = null;

        this._enabled = false;
    },

    enabled: function(){
        return this._enabled;
    },

});

var History = Backbone.Collection.extend({

    model: app.Era,

    localStorage: new Backbone.LocalStorage('karass-history'),

    enabled: function(){
        return this.filter(function(era){
            return era.enabled();
        }); 
    },

    nextOrder: function(){
        if(!this.length){
            return 1;
        }

        return this.last().get('order') + 1;
    },

    comparator: function( era ) {
        return era.get('order');
    },

    comparatorFrom: function( era ){
        return era.get('from');
    },

    comparatorUntil: function(era){
        return era.get('until');
    },

    getFirstFrom: function(){
        // todo
    },

    getLastUntil: function(){
        // todo
    },

    getEraAt: function(time){
        // todo
    },

});

app.History = new History();

The Views

app.AppView = Backbone.View.extend({

    // bind to the karassApp div we set up in the html file
    el: "#karassApp", 

    // Our template for the line of statistics at the bottom of the app.
    statsTemplate: _.template( $('#stats-template').html() ),

    // delegated events for creating new items, and clearing completed ones
    events: {
        'keypress #new-era-start': 'focusOnEnd',
        'keypress #new-era-end': 'createEraOnEnter',
        'click #disable-history': 'toggleEnabledAllEras'
    },

    // At initialization we bind to the relevant events on the 'eras' 
    // collection, when items are added or changed. Kick things off by
    // loading any preexisting todos that might be saved in *localStorage*
    initialize: function(){
        //_.bindAll(this);

        this.era_start = this.$('#new-era-start');
        this.era_end = this.$('#new-era-end');
        this.disableHistory = this.$('#disable-history');
        this.$footer = this.$('#footer');
        this.$main = this.$('#main');

        window.app.History.on('add', this.addOneEra, this);
        window.app.History.on('reset', this.addAllEras, this);
        // window.app.History.on('add:true', this.addAllEras, this);
        window.app.History.on('change:_enabled', this.filterOneEra, this);
        window.app.History.on('filter', this.filterAllEras, this);

        window.app.History.on('all', this.renderHistory, this);

        app.History.fetch();
    },

    // Re-rendering the App just means refreshing the statistics -- the rest
    // of the app doesn't change.
    renderHistory: function(){
        var era_count = app.History.length();

        if(app.History.length){
            this.$main.show();
            this.$footer.show();

            this.$footer.html(this.statsTemplate({
                era_count: era_count,
            }));

            this.$('#filters li a')
                .removeClass('selected')
                .filter('[href=#/' + (app.EraFilter || '' ) + '"]')
                .addClass('selected');

        }else{
            this.$main.hide();
            this.$footer.hide();
        }

        this.disableHistory = !app.History.enabled();
    },

    // Add a single era item to the list by creating a view for it, and
    // appending its element to the `<ul>`.
    addOneEra: function(era){
        var view = new app.EraView({ model: era });
        $('#history').append(view.render().el);
    },

    addAllEras: function(){
        this.$('#history').html('');
        app.History.each(this.addOneEra, this);
    },

    filterOneEra: function(era){
        era.trigger('visible');
    },

    filterAllEras: function(){
        app.History.each(this.filterOne, this);
    },

    // Generate the attributes for the new Era Item
    newEraAttributes: function(){
        return {
            from: this.era_start.val().trim(), // validation logic should probably go here: make sure it's a date that can be saved
            until: this.era_end.val().trim(), // ditto
            stash: {
                from: null,
                until: null
            },
            order: app.History.nextOrder(),
            _enabled: true,
        }
    },

    focusOnEnd: function(e){
        if(e.which !== ENTER_KEY || !this.era_start.val().trim()){
            return;
        }

        this.era_end.focus();
    },

    createEraOnEnter: function(e){
        if(e.which !== ENTER_KEY || !this.era_start.val().trim()){
            return;
        }

        app.History.create(this.newEraAttributes());
        this.era_start.val('');
        this.era_end.val('');
    },

    toggleEnabledAllEras: function(){
        var enabled = !this.disableHistory.checked;

        app.History.each(function(era){
            era.toggle();
            era.save();
        });
    }
});

app.EraView = Backbone.View.extend({
    tagName: 'li',
    template: _.template( $('#era-template').html() ),

    // The DOM events specified to an item
    events: {
        'dblclick label': 'edit',
        'keypress .edit .start': 'focusOnEnd',
        'keypress .edit .end': 'updateOnEnter',
        'blur .edit': 'close',
    },

    // The EraView listens for changes to its model, re-rendering. Since there's
    // a one-to-one correspondence between an era and a EraView in this app, 
    // we set a direct reference on the model for convenience.
    initialize: function(){
        //_.bindAll(this);
        this.model.on('change', this.render, this);
    },

    // Re-renders the era item to the current state of the model and
    // updates the reference to the era's edit input within the view
    render: function(){
        this.$el.html( this.template(this.model.toJSON()));
        this.era_start = this.$('.era-start');
        this.era_end = this.$('.era-end');
        return this;
    },

    // Switch this view into editing mode, displaying the input field
    edit: function(){
        this.$el.addClass('editing');
        this.era_start.focus();
    },

    // Close the editing mode, saving changes to the era
    close: function(){
        var start = this.era_start.val().trim();
        var end = this.era_end.val().trim();

        if(start && end){
            this.model.save({from: start, until: end});
        }

        this.$el.removeClass('editing');
    },

    focusOnEnd: function(e){
        if(e.which !== ENTER_KEY || !this.era_start.val().trim()){
            return;
        }

        this.era_end.focus();
    },

    updateOnEnter: function(e){
        if(e.which !== ENTER_KEY || !this.era_end.val().trim()){
            return;
        }

        this.close();
    }
});

And last but not least:

The HTML

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Backbone.js • HistoryMVC</title>
</head>
<body>
    <section id="karassApp">
        <header id="header">
            <h1>Karass</h1>
            <input id="new-era-start" placeholder="Since when?" autofocus>
            <input id="new-era-end" placeholder="Until when?" >
        </header>
        <section id="main">
            <input id="disable-history" type="checkbox">
            <label for="disable-history">Disable History, work on Snapshot instead</label>
            <ul id="history"></ul>
        </section>
        <footer id="footer"></footer>
    </section>
    <div id="info">
        No info here, traveller!
    </div>

    <script type="text/template" id="era-template">
        <li>
            <input class="era-start" placeholder="Since when?" value="<%= from %>">
            <input class="era-end" placeholder="Until when?" value="<%= until %>">
            <label>Edit</label>
        </li>
    </script>

    <script type="text/template" id="stats-template">
        <% if (era_count) { %>
            <p>There are <%= era_count %> eras to display</p>
        <% }else{ %>
            <p>There are no eras to display, yet.</p>
        <% } %>
    </script>

    <script src="js/lib/jquery-1.9.0.min.js"></script>
    <script src="js/lib/underscore-min.js"></script>
    <script src="js/lib/backbone.js"></script>
    <script src="js/lib/backbone.localStorage-min.js"></script>
    <script src="js/models/era.js"></script>
    <script src="js/models/history.js"></script>
    <script src="js/views/app.js"></script>
    <script src="js/views/era.js"></script>
</html>

Debug

I’m able to instantiate an Era using era = new app.Era({ from: 0, until: 1, stash: {from: null, until: null}, _enabled: true});, but calling app.History.create(...) with the same arguments throws the error.

Question
What is the error here? I tried to do everything anologous to the tutorial, and have been over all parts of the code three times now to see if I would spot an error.

Thank you!

Oh yeah, and credit goes to Nyxynyx, whose question I shamelessly ripped off after not finding an answer to mine.

  • 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-17T15:41:08+00:00Added an answer on June 17, 2026 at 3:41 pm

    That’s a bug in Backbone.localStorage, which isn’t compatible with Backbone 0.9.10.

    There’s a pull request on Github, in the meantime replace Backbone.LocalStorage with this version.

    • 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&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
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 &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to create an if statement in PHP that prevents a single post

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.