【问题标题】:backbone.js a url property must be defined主干.js 必须定义一个 url 属性
【发布时间】:2013-02-02 15:30:10
【问题描述】:

我正在尝试使用 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: Names 集合如下所示:

karass.Names = History.extend({

    model: karass.Name,

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

});

编辑3:我把所有代码都贴在了jsfiddle上:http://jsfiddle.net/herrturtur/CRv6h/

【问题讨论】:

    标签: backbone.js backbone-model


    【解决方案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 丢失异常。

    【讨论】:

    • 是的,做到了。 (也应该发布 Names 集合代码)
    • 等等,时代不是收藏。它是由 Name 扩展的模型,而 Name 又进入 Names 集合。此 Names 集合定义了 localStorage 属性。
    • 对不起,但我没有关注你的哪个类扩展什么和属于哪个集合......从你的EraView 中的对象this.model 中的对象的异常消息中是真的不具有localStorage 属性,也不属于具有localStorage 的集合。您应该仔细检查您的所有集合都有localStorage,并且只有将模型传递给模型在集合中的视图时才可以。
    • 否则您需要发布完整的模型/集合层次结构以及作为模型传递到 EraView 的内容...
    • 时代扩展模型;名字延续时代; Names:Name的集合,具有localStorage属性;所以你是说每个模型都必须属于一个系列? EraView 中的 this.model 基本上是一个 Name 对象,属于具有 localStorage 属性的集合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多