0

index.htmlでこれを実行すると、次のエラーが発生します:「UncaughtSyntaxError:Unexpected token:」、参照

events: {
            "click #add-friend": "showPrompt",
        },

具体的には、ここで「:」を参照します。「#add-friendをクリックしてください」:「showPrompt」以下の詳細なコンテキスト。何かアドバイスをいただければ幸いです。

(function ($) {

    Friend = Backbone.Model.extend({
        // create a model to to hold friend attribute
        name: null
    });

    Friends = Backbone.Collection.extend({
        // this is our friends collection and holds out Friend models
        initialize: function(models, options) {
            this.bind("add", options.view.addFriendLi);
            // listens for "add" and calls a view function if so
        }
    });

    AppView = Backbone.View.extend({
        el: $("body"),
        initialize: function () {
            this.friends = new Friends(null, {view: this});
        // creates a new friends collection when the view is initialized
        // pass it a reference to the view to create a connection between the two
        events: {
            "click #add-friend": "showPrompt"
        },
        showPrompt: function () {
            var friend_name = prompt("Who is your friend?");
            var friend_model = new Friend({name:friend_name});
            // adds a new friend model to out Friend collection
            this.friends.add(friend_model);
        },
        addFriendLi: function (model) {
            // the parameter passed is a reference to the model that was added
            $("#friends_list").append("<li>" + model.get('name') + "</li>");
        }
    });
    var appview = new AppView;
})(jQuery);
4

3 に答える 3

2

最後に余分なコンマがあります:

"click #add-friend": "showPrompt" // remove the comma

}また、initializeメソッドの最後にクロージングがありません。

initialize: function () {
    this.friends = new Friends(null, {view: this});
}, // add a "}," here

events: {
   "click #add-friend": "showPrompt"
},
于 2012-03-23T15:45:21.900 に答える
2

「初期化」関数の「}」がありません。それがないと、トークン「イベント」が新しいステートメントを開始していると見なされます。文字列定数の後のコロンまではすべて問題ありませんが、そのコンテキストでは構文的に正しくありません。

また、「initialize」プロパティの値を「events」プロパティから分離するために、そこにもコンマが必要です。

于 2012-03-23T15:45:30.867 に答える
1

プロパティ値の後のコンマを削除します。

events: {
     "click #add-friend": "showPrompt" // <-- comma removed!
},
于 2012-03-23T15:45:20.683 に答える