lowerkey
lowerkey

Reputation: 8335

backbone.js a url property must be defined

I'm trying to use Backbone.localStorage as a backend to an app.

I wrote a Model called Era, and a corresponding EraView. Pressing enter in the EraView causes the model to be saved, and this is where I get the error:

Uncaught Error: A "url" property or function must be specified 
urlError     backbone.js:1509
_.extend.url     backbone.js:515
_.result     underscore-min.js:1060
Backbone.sync     backbone.js:1410
Backbone.sync     backbone.localStorage.js:188
_.extend.sync     backbone.js:276
_.extend.save     backbone.js:476
karass.EraView.Backbone.View.extend.close     era.js:61
karass.EraView.Backbone.View.extend.updateOnEnter     era.js:75

Here is the code to the EraView

var karass = karass || {};

// Era Item View

// the DOM element for an era item
karass.EraView = Backbone.View.extend({
    tagName: 'li',
    className: 'era',
    template: _.template( $('#era-template').html() ),

    // The DOM events specified to an item
    events: {
        'dblclick .edit-input': 'edit',
        'keypress .edit-input': '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 karass, 
    // 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.attributes));
        this.$era_start = this.$('.era-start');
        this.$era_end = this.$('.era-end');

        this.$era_start.attr('disabled', true);
        this.$era_end.attr('disabled', true);

        return this;
    },

    // Switch this view into editing mode, displaying the input field
    edit: function(){
        this.$('.edit-input').removeAttr('disabled');

        this.$el.addClass('editing');
        this.$('.edit-input').addClass('editing');
    },

    // Close the editing mode, saving changes to the era
    close: function(){
        this.$('.edit-input').attr('disabled', true);

        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');
        this.$('.edit-input').removeClass('editing');

        this.trigger('close');
    },

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

        this.close();
    }
});

And this is the code for the era model:

var karass = karass || {};

karass.Era = Backbone.Model.extend({

    defaults: {
        from: '', 
        until: ''
    },

});

I thought I didn't need a url while using localStorage.

Edit: I forgot to mention that while this behavior occurs in the Era/EraView itself, it also occurs in the Name model, which extends Era. Name in turn belongs in a Names collection. I don't know if this makes a difference, but I figured I add it.

Edit 2: The Names collection looks like this:

karass.Names = History.extend({

    model: karass.Name,

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

});

Edit 3: I posted all the code on jsfiddle: http://jsfiddle.net/herrturtur/CRv6h/

Upvotes: 1

Views: 1276

Answers (1)

nemesv
nemesv

Reputation: 139758

You don't need an url while using localStorage. But you need to set the localStorage property on your model or on your collection (if you set the localStorage on a collection the models inside the collection will "inherit" this setting):

karass.Era = Backbone.Model.extend({

    localStorage: new Backbone.LocalStorage("EraCollection"), 
    // the EraCollection should be an unique name within your app.

    defaults: {
        from: '', 
        until: ''
    },

});

If you don't setup the localStorage property the plugin falls back to the default ajax sync so you get the uri missing exception.

Upvotes: 1

Related Questions