Backbone localstorage必须指定“url”属性或函数

时间:2013-05-30 12:41:31

标签: backbone.js local-storage

我正在使用backbone-localstorage插件创建一个基于骨干示例的小型应用程序。

保存新模型的数据时,我总是会收到错误“ A”url“必须指定属性或函数

在阅读了几个类似的主题后,我仍然无法找到原因。

型号:

directory.models.EmployeeCollection = Backbone.Collection.extend({

    localStorage: new Backbone.LocalStorage("EmployeeCollection"), 

    model: directory.models.Employee,

    store: directory.utils.store,

    findByName: function(key) {
        this.reset(this.store.findByName(key));
    }

});

观点:

directory.views.newEmployeeView = Backbone.View.extend({

    tagName: "div",

    initialize: function() {
        this.template = _.template(directory.utils.templateLoader.get('new-employee'));
    },

    events: {
        "click .save": "saveEmployee"
    },

    render: function(eventName) {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },

    saveEmployee: function(event){
        this.model.set({
            firstName:$('#newFirstName').val(),
            lastName:$('#newLastName').val(),
            title:$('#newTitle').val(),
            city:$('#newCity').val(),
            officePhone:$('#newOfficePhone').val(),
            cellPhone:$('#newCellPhone').val(),
            email:$('#newEmail').val()
        });

        this.model.save();
        window.history.back();

        return false;
    }
});

1 个答案:

答案 0 :(得分:3)

我认为在尝试保留它之前,您需要将新模型作为您的集合的成员。尝试创建集合的新实例并将其传递给视图(可能在您的路由器中),如下所示:

new newEmployeeView({ collection: new EmployeeCollection() });

在您的视图中,您可以使用Backbone的创建便捷方法(请参阅docs)将新模型实例添加到集合中并保留它:

this.collection.create({
    firstName:$('#newFirstName').val(),
    lastName:$('#newLastName').val(),
    title:$('#newTitle').val(),
    city:$('#newCity').val(),
    officePhone:$('#newOfficePhone').val(),
    cellPhone:$('#newCellPhone').val(),
    email:$('#newEmail').val()
});
相关问题