Backbone.js多次触发事件,如何取消绑定事件?

时间:2014-05-02 10:10:44

标签: javascript jquery backbone.js backbone-events

我正在运行问题。如何在特定视图中取消绑定事件。我尝试了unbindundelegate,但问题仍未解决。

var PostwallView = Backbone.View.extend({
    el: $("#content"),
    events: {
        'click #postinwall' : 'post message', // my events 
    },

    initialize: function() {
        var that = this;
        var options = {query: uni_id+"/chaid/"+currentChallenge['id']}
        var onDataHandler = function(collection) 
        { 
            //onDataHandler is working on success full api load           
            that.render();  // calling my render 
            $('#postinwall').unbind('click',postmessage);

        }
        var onErrorHandler = function(collection) 
        {  **// if api fail**
            var errorstring = JSON.stringify(collection);
            console.log(errorstring);
        }

        this.model = new CommentinfoModel(options);  
        // this model to fetch the data from api if api success then i am call onDataHandler and inside render ()

        this.model.fetch({ success : onDataHandler,error:onErrorHandler,dataType: "json"});
    },

    render: function() {
        $('.nav li').removeClass('active');
        $('.nav li a[href="'+window.location.hash+'"]').parent().addClass('active');
        var data = {
            cinfo: this.model.toJSON(),
            _: _
        };
        var compiledTemplate = _.template( PostwallTemplate,{  data :  data });
        this.$el.html(compiledTemplate);
    },

    postmessage: function(e){   // bind event fire more than on time when i am coming from   some other event to call this events . 
        if ($('#txt').val() == '') {
            alert('post not be empty!');
        }
        else {                                                               
            alert("here");
            e.preventDefault();                           
        },               
    });

    return PostwallView;
});

0 个答案:

没有答案