0

I have the following code outside the scope of my view model and i am attempting to call it with a click binding.

function Policy(data) {
    var self = this;
    Object.keys(data).forEach(function(prop) {
        self[prop] = data[prop];
    });

    var generate = function() {
        console.log("test");
        // window.open("www.google.com")
        // window.open("{{ url_for('genreport') }}/" + qvm.letter() + '/' + self.id);
    }

}

however, when i try to call the function i get that generate is not defined. the code for my binding is below

<button class="btn btn-lg btn-primary btn-block" data-bind="click: function(){ generate() }">Generate</button>

I have tried calling Policy.generate, $data.generate, and i cannot get this function to call.

I know this issue is simple and im probably missing something that should be smashing me in the face but i'm oblivious, any help would be appreciated.

2 Answers 2

1

The fix was just to place the said function within the scope of my ViewModel, it is now fixed.

0

You have declared generate using a local variable. It is not visible outside the scope of Policy. If you want to create Policy.generate, you should do that:

Policy.generate = function () {
1
  • I was just about to update the question saying i had fixed it by moving it within the scope of the view model not sure why i placed it outside to begin with, thank you for your comment.
    – Trevor
    Commented Jan 24, 2018 at 23:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.