0

我在使用 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

我试图返回并尽可能接近地复制代码,但我仍然无法让它工作。有什么建议么?

4

1 回答 1

3

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

您需要一个对象来附加您的视图 - JavaScript 没有活力

于 2013-03-01T18:07:02.187 回答