Here's a sample piece of JS for Knockout:
function AppViewModel() {
this.firstName = ko.observable('Bob');
this.lastName = ko.observable('Smith');
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
}
In case you're unfamiliar with KO, each field on AppViewModel becomes a function (i.e. ko.observable and ko.computed each return a function. Also note that fullName depends on both functions.
How can I rewrite this to ClojureScript?
One thing to try is:
(deftype AppViewModel [firstName lastName]
Object
(fullName [this] (.computed js/ko (str (firstName) " " (lastName)))))
(defn my-model [first last]
(AppViewModel.
(.observable js/ko "Bob")
(.observable js/ko "Smith")))
It doesn't work though, since fullName becomes a function that calls ko.computed. That is, it compiles to:
my_namespace.AppViewModel.prototype.fullName = function() {
return ko.computed([cljs.core.str(this.firstName.call(null)), cljs.core.str(" "), cljs.core.str(this.lastName.call(null))].join(""))
};
How can I deal with it in ClojureScript?
Again, note the dependency of fullName on this and firstName/lastName.
.computed. In your JavaScript example you pass a function and a reference. - eagleflo