how to use a variable on Backbone view ?

If i work like this (below)... This does not work.

App.Views.Incentive = Backbone.View.extend({

        el: $('#projet-page'),
        name: "Incentive",
        initialize: function () {
            "use strict";
            $('section[data-projet-page="' + this.name + '"] a.OpenGalerie').on('click', this.enterGalerieClick);
        },
        enterProjectClick: function () {
            Backbone.history.navigate('!/case-projet/' + this.name + '/Galerie'),
            $('section[data-projet-page="' + this.name + '"]').css('display', 'block'),
        },

    });

i have :

domain:9000/#!/case-projet//Galerie instead of domain:9000/#!/case-projet/Incentive/Galerie

有帮助吗?

解决方案

You can solve the immediate problem by using underscore's bind method (see documentation) to ensure that this in the click handler refers to the Backbone view and not the element you've bound the handler to:

App.Views.Incentive = Backbone.View.extend({

        el: $('#projet-page'),
        name: "Incentive",
        initialize: function () {
            "use strict";
            $('section[data-projet-page="' + this.name + '"] a.OpenGalerie').on('click', _.bind(this.enterGalerieClick, this));
        },
        enterProjectClick: function () {
            Backbone.history.navigate('!/case-projet/' + this.name + '/Galerie'),
            $('section[data-projet-page="' + this.name + '"]').css('display', 'block'),
        },

    });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top