0

I have this base view model:

var baseViewModel = function () {
    var self = this;

    // <!----- AJAX SAVING ------!> \\
    self.saving = ko.observable();

    // <!----- SEARCHING ------!> \\
    self.fields = ko.observableArray();
    self.selectedField = ko.observable();
    self.searchTerm = ko.observable().extend({ throttle: 150 });
}

And I inherit it using this:

var viewModel = function () {
    baseViewModel.call(this);
    var self = this;

    //stufff
}
viewModel.prototype = new baseViewModel();

And it works perfectly. Quite pleased with it.

Now, I want to setup the self.fields property with some initial data, that I want to send through the line baseViewModel.call(this) and I'm not sure whether to do this:

var viewModel = function () {
    baseViewModel.call(this, new userModel()); // just a function object
    var self = this;
}

OR:

var viewModel = function () {
    baseViewModel.apply(this, new userModel()); // just a function object
    var self = this;
}

So that the baseViewModel will do this:

var baseViewModel = function (data) {
    var self = this;

    // <!----- AJAX SAVING ------!> \\
    self.saving = ko.observable();

    // <!----- SEARCHING ------!> \\
    self.fields = ko.observableArray().getKeys(data); // viewModel parameter passed here
    self.selectedField = ko.observable();
    self.searchTerm = ko.observable().extend({ throttle: 150 });
}

I have read this Difference between call and apply still not sure where to go and I have read the official documentation.

EDIT

I have just tried call because as I understand it the only difference is either putting in a bunch or args (with call) or putting in an array of args (with apply)

Its worked with call so far, just wondering if there are going to be any caveats with choosing this method?

Community
  • 1
  • 1
Callum Linington
  • 13,082
  • 10
  • 61
  • 132

1 Answers1

0

Unless there are any caveats, the only difference is whether the args come as and array or separate objects

Call Link

Apply Link

with call you do baseViewModel.call(this [, arg1, arg2, .... argn])

with apply you do baseViewModel.apply(this [, arg_array[] ])

Callum Linington
  • 13,082
  • 10
  • 61
  • 132