1

我正在尝试使用 Backbone.localStorage 作为应用程序的后端。

我编写了一个名为 Era 的模型,以及一个相应的 EraView。在 EraView 中按 enter 会导致模型被保存,这就是我得到错误的地方:

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

这是 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();
    }
});

这是时代模型的代码:

var karass = karass || {};

karass.Era = Backbone.Model.extend({

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

});

我以为我在使用 localStorage 时不需要 url。

编辑:我忘了提到,虽然这种行为发生在 Era/EraView 本身中,但它也发生在扩展 Era 的 Name 模型中。Name 又属于 Names 集合。我不知道这是否有区别,但我想我添加它。

编辑 2:名称集合如下所示:

karass.Names = History.extend({

    model: karass.Name,

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

});

编辑 3:我在 jsfiddle 上发布了所有代码:http: //jsfiddle.net/herrturtur/CRv6h/

4

1 回答 1

1

使用 localStorage 时不需要 url。但是您需要localStorage在模型或集合上设置属性(如果在集合localStorage上设置,集合内的模型将“继承”此设置):

karass.Era = Backbone.Model.extend({

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

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

});

如果您不设置该localStorage属性,则插件会退回到默认的 ajax 同步,因此您会得到 uri 丢失异常。

于 2013-02-02T16:02:39.190 回答