Trevor
Trevor

Reputation: 65

Call a function outside of ViewModel scope

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.

Upvotes: 0

Views: 44

Answers (2)

Trevor
Trevor

Reputation: 65

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

Upvotes: 1

Roy J
Roy J

Reputation: 43881

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 () {

Upvotes: 0

Related Questions