javascript - BackboneJS: How to call function outside view from view's events hash? - Stack Overflow

As an example, here's what I'm looking to do...function doSomething(customParameter) {console

As an example, here's what I'm looking to do...

function doSomething(customParameter) {
    console.log(customParameter);
}

var MyView = Backbone.View.extend({
    // Other view stuff, skipping it...
    events: {
        'click .someClass': doSomething(customParameter)
    }
});

The motivation here is I want to be able to pass variables (local to the view) to some other function outside the view. The reason I want to do this is so that the "other function" is only written once and not four times...it would seriously save my project over 100 lines of code.

After much googling I haven't found a solution to this...any ideas?

Thanks!

As an example, here's what I'm looking to do...

function doSomething(customParameter) {
    console.log(customParameter);
}

var MyView = Backbone.View.extend({
    // Other view stuff, skipping it...
    events: {
        'click .someClass': doSomething(customParameter)
    }
});

The motivation here is I want to be able to pass variables (local to the view) to some other function outside the view. The reason I want to do this is so that the "other function" is only written once and not four times...it would seriously save my project over 100 lines of code.

After much googling I haven't found a solution to this...any ideas?

Thanks!

Share Improve this question asked Dec 11, 2013 at 19:30 MattMMattM 1,2891 gold badge14 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Here is one way of doing it:

function doSomething(customParameter) {
    console.log(customParameter);
}

var MyView = Backbone.View.extend({
    // Other view stuff, skipping it...
    events: {
        'click .someClass': function(ev) { doSomething(this.customParameter); }
    }
});

In this example, you declare a function that passes the arguments needed from the local view to that doSomething method.


Another way could be to call the doSomething() with the context of the current view, if you don't want to pass all the required arguments:

function doSomething(ev) {
    console.log(this.customParameter); // here, the context this will be the view.
}

var MyView = Backbone.View.extend({
    // Other view stuff, skipping it...
    events: {
        'click .someClass': function(ev) { doSomething.call(this, ev); }
    }
});

A more OOP approach would be to have a base View for that mon code:

var MyBaseView = Backbone.View.extend({
    doSomething: function(ev) {
        console.log(this.customParameter);
    }
});

var MyView = MyBaseView.extend({
    extends: {
        'click .someClass': 'doSomething'
    }
});

Probably there are more ways of doing it, you just have to see which scenario applies best to your needs.

You can try calling a method that is internal to the view which calls the method in question

var MyView = Backbone.View.extend({
    // Other view stuff, skipping it...
    events: {
        'click .someClass': 'callSomething'
    },
    callSomething: function() {
        doSomething(customParameter);
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745106137a4611565.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信