不能设置property 'new_form'的定义

Cannot set property 'new_form' of undefined

本文关键字:form 定义 new 设置 不能 property      更新时间:2023-09-26

我遇到了来自Treehouse的Backbone.js教程的麻烦。下面是我的代码:

var NotesApp = (function () {
    var App = {
        stores: {}
    }
    App.stores.notes = new Store('notes');
    // Note Model
    var Note = Backbone.Model.extend({
        //Local Storage
        localStorage: App.stores.notes,
        initialize: function () {
            if (!this.get('title')) {
                this.set({
                    title: "Note at " + Date()
                })
            };
            if (!this.get('body')) {
                this.set({
                    body: "No Body"
                })
            };
        }
    })
    //Views
    var NewFormView = Backbone.View.extend({
        events: {
            "submit form": "createNote"
        },
        createNote: function (e) {
            var attrs = this.getAttributes(),
                note = new Note();
            note.set(attrs);
            note.save();
        },
        getAttributes: function () {
            return {
                title: this.$('form [name=title]').val(),
                body: this.$('form [name=body]').val()
            }
        }
    });
    window.Note = Note;
    $(document).ready(function () {
        App.views.new_form = new NewFormView({
            el: $('#new')
        });
    })
    return App
})();

我得到错误:Cannot set property 'new_form' of undefined

我试着回去尽可能地复制代码,但我仍然不能让它工作。有什么建议吗?

stores: {}后添加, views: {}

你需要一个对象来附加你的视图- JavaScript没有可视