Knockout Computed Observable with parameters

ioseph

Is it possible to provide a computed observable an extra parameter?

For example, something like this:

var ViewModel = function(first, last) {
    this.firstName = ko.observable(first);
    this.lastName = ko.observable(last);
    var self = this;
    this.fullName = ko.computed(function(separator) {
       return self.firstName() + ' ' + self.lastName();
    }, this);
};

And then in the html:

<div data-bind="text: fullName(' - ')"></div>

My actual use case is far more complicated, but this is essentially what I'm trying to achieve, pass in a value in the html which is used as part of the computed function.

Failing this is there a way to make a ordinary function which takes parameters behave like a (computed) observable?

Anuraj

You can create a function, which returns an computed variable. You can try something like this.

var ViewModel = function(first, last) {
    this.firstName = ko.observable(first);
    this.lastName = ko.observable(last);
    var self = this;
    this.fullName = function(separator){
    return ko.computed(function () {
            return self.firstName() + separator + self.lastName();}, this);
};
};

<div data-bind="text: ViewModel.fullName('-')"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

knockout observable array computed

From Dev

observable and computed are not working properly Knockout

From Dev

Knockout computed observable with access to observableArray

From Dev

Inheritance and overriding Knockout computed observable

From Dev

Inheritance and overriding Knockout computed observable

From Dev

Knockout pushing observable and computed data to an observable array

From Dev

Knockout.js: Subscribe or computed observable?

From Dev

Knockout computed not updating using observable array

From Dev

Knockout : find out which observable triggerred computed

From Dev

Knockout.js: computed observable not updating as expected

From Dev

Knockout chained computed observable throws exception on update

From Dev

Knockout computed creates dependency for observable that is written to but not read

From Dev

knockout.js observable groups/computed groups

From Dev

Knockout JS: Computed observable failing to update

From Dev

How to make 'independent' computed observable of some observable (Knockout.js)

From Dev

Accessing Knockout Observable Property from Computed Property Gives TypeError

From Dev

Knockout computed observable is not updating value when changing its dependencies

From Dev

What is the analog for Knockout's writable computed observable in AngularJS?

From Dev

Reevaluating a Knockout computed which depends just on an observable array

From Dev

Data context for computed observable function in Knockout.js

From Dev

Reevaluating a Knockout computed which depends just on an observable array

From Dev

How to use index in computed observable [knockout.js]

From Dev

Knockout - Difference between ko.computed and ko.observable.subscribe

From Dev

Knockout JS Computed Observable also takes User input

From Dev

Running a function every time a computed observable changes Knockout

From Dev

Knockout: can observable extenders be called without parameters?

From Dev

knockout computed array not updating when function updates observable array used in computed

From Dev

Knockout Computed with Parameter not updated

From Dev

Knockout writable computed observables

Related Related

  1. 1

    knockout observable array computed

  2. 2

    observable and computed are not working properly Knockout

  3. 3

    Knockout computed observable with access to observableArray

  4. 4

    Inheritance and overriding Knockout computed observable

  5. 5

    Inheritance and overriding Knockout computed observable

  6. 6

    Knockout pushing observable and computed data to an observable array

  7. 7

    Knockout.js: Subscribe or computed observable?

  8. 8

    Knockout computed not updating using observable array

  9. 9

    Knockout : find out which observable triggerred computed

  10. 10

    Knockout.js: computed observable not updating as expected

  11. 11

    Knockout chained computed observable throws exception on update

  12. 12

    Knockout computed creates dependency for observable that is written to but not read

  13. 13

    knockout.js observable groups/computed groups

  14. 14

    Knockout JS: Computed observable failing to update

  15. 15

    How to make 'independent' computed observable of some observable (Knockout.js)

  16. 16

    Accessing Knockout Observable Property from Computed Property Gives TypeError

  17. 17

    Knockout computed observable is not updating value when changing its dependencies

  18. 18

    What is the analog for Knockout's writable computed observable in AngularJS?

  19. 19

    Reevaluating a Knockout computed which depends just on an observable array

  20. 20

    Data context for computed observable function in Knockout.js

  21. 21

    Reevaluating a Knockout computed which depends just on an observable array

  22. 22

    How to use index in computed observable [knockout.js]

  23. 23

    Knockout - Difference between ko.computed and ko.observable.subscribe

  24. 24

    Knockout JS Computed Observable also takes User input

  25. 25

    Running a function every time a computed observable changes Knockout

  26. 26

    Knockout: can observable extenders be called without parameters?

  27. 27

    knockout computed array not updating when function updates observable array used in computed

  28. 28

    Knockout Computed with Parameter not updated

  29. 29

    Knockout writable computed observables

HotTag

Archive