Backbone эта путаница

У меня есть следующий код:

var GoalPanelView = Backbone.View.extend({

    // Bind to the goal panel DOM element
    el: $("#sidebar-goals"),    

    // Initialize the collection
    initialize: function() {
        this.collection = Goals;
        this.collection.bind('add', this.appendItem);
    },

    // Create a new goal when a user presses enter in the enter goal input
    createOnEnter: function(e) {
        if (e.keyCode != 13) return;
        this.addItem();
        //Goals.create(this.newAttributes());           
    },

    // Add the goal item to the goal list
    addItem: function() {
        var goal = new Goal();
        goal.set(this.newAttributes());
        var goalsElem = this.el;
        this.collection.add(goal);
        $(this.el).children("#enter-goal").val('');
    },

    // Append DOM element to the parent el
    appendItem: function(item) {
        var goalView = new GoalView({
            model: item,
        });
        $(this.elem).append(goalView.render().el);
    }

});

Моя проблема находится внутри функции appendItem.Когда я использую thisвнутри appendItem , я полагаю, что она считает, что thisотносится к this.collection, а не к GoalPanelView. Как мне получить thisдля ссылки на GoalPanelView, а не на collection. Я попытался передать другую переменную в функцию appendItem, которая содержала содержимое this.elem, но, похоже, это не сработало.

Одна вещь, которая сработала, была, когда я переместил функцию appendItemв collectionи изменил инициализацию на привязать к this.collection.bind('добавить' , appendItem);, но я не хочу помещать материал viewв логику collection.

5
задан ElChiniNet 19 January 2016 в 22:32
поделиться