Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:05:46+00:00 2026-06-05T11:05:46+00:00

I’m trying to wrap knockout.js in clojurescript but its turning to be very difficult.

  • 0

I’m trying to wrap knockout.js in clojurescript but its turning to be very difficult. The problem that I’m having is the reference to the ‘this’ variable. I’m thinking of giving up and just using javascript directly.

I’ve taken examples off http://knockoutjs.com/examples/helloWorld.html and http://knockoutjs.com/examples/contactsEditor.html

I’ve managed to wrap easy functions with some macros. For example:

var ViewModel = function() {
    this.firstName = ko.observable("Bert");
    this.lastName = ko.observable("Bertington");

    this.fullName = ko.computed(function() {
        // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
        return this.firstName() + " " + this.lastName();
    }, this);
};

becomes:

(defviewmodel data
  (observing :first_name "Bert")
  (observing :last_name  "Bertington")
  (computing :name [:first_name :last_name]
    (str :first_name " " :last_name)))

However, for something harder like:

var BetterListModel = function () {
    this.itemToAdd = ko.observable("");
    this.allItems = ko.observableArray(["Fries", "Eggs Benedict", "Ham", "Cheese"]); // Initial items
    this.selectedItems = ko.observableArray(["Ham"]);                                // Initial selection

    this.addItem = function () {
        if ((this.itemToAdd() != "") && (this.allItems.indexOf(this.itemToAdd()) < 0)) // Prevent blanks and duplicates
            this.allItems.push(this.itemToAdd());
        this.itemToAdd(""); // Clear the text box
    };

    this.removeSelected = function () {
        this.allItems.removeAll(this.selectedItems());
        this.selectedItems([]); // Clear selection
    };

    this.sortItems = function() {
        this.allItems.sort();
    };
};

ko.applyBindings(new BetterListModel());

I’m not sure what I can do in clojurescript to match code like this: this.allItems.push(this.itemToAdd())

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:05:49+00:00Added an answer on June 5, 2026 at 11:05 am

    After lots of trial and error, I figured out how to have the same structure for clojurescript as for javascript.

    The this-as macro has a few idiosyncrasies and only works when the method is put into the class

    for example I want to create something that looks like this in javascript:

    var anobj = {a: 9,
                 get_a: function(){return this.a;}};
    

    I have to do a whole lot more coding to get the same object in clojurescript:

    (def anobj (js-obj))
    (def get_a (fn [] (this-as me (.-a me))))
    (aset anobj "a" 9)
    (aset anobj "get_a" get_a)
    

    which is seriously ugly for a language as beautiful as clojure. Things get worse when you have got functions that link to each other, like what happens in knockout.

    I found that the best way to create an js-object with lots of this‘s in there is to define a __init__ method, add it to the class and then run it, then remove it from the class. For example, if I wanted to make another object:

    var avobj = {a: this,
                 b: 98,
                 c: this.a
                 get_a: function(){return str(this.a) + str(this.c);}};
    

    written as clojurescript with and __init__ method looks like this:

    (def avobj (js-obj))
    (def av__init__ 
         #(this-as this 
            (aset this "a" this) 
            (aset this "b" 9) 
            (aset this "c" (.-a this))
            (aset this "get_a" (fn [] (str (.-a this) (.-c this))))))
    (aset avobj "__init__" av__init__)
    (. avobj __init__)
    (js-delete stuff "__init__")
    

    There’s still a whole bunch more code than javascript… but the most important thing is that you get the same object as javascript. Setting all the variables using this form also allows the use of macros to simplify. So now I have defined a macro:

    (defmacro defvar [name & body]
       (list 'do
        (list 'def name
          (list 'map->js 
            {
              :__init__ 
              (list 'fn []
                  (list 'this-as 'this
                     (list 'aset 'this "a" "blah")))          
            }))
        ;(. js/console log ~name)
        (list '. name '__init__)
        (list 'js-delete name "__init__")))
    

    and with map->js taken from jayq.utils:

    (defn map->js [m]
      (let [out (js-obj)]
        (doseq [[k v] m]
          (aset out (name k) v))
        out))
    

    Now I can write code like this:

    (defvar avobj
       a this 
       b 9 
       c (.-a this)
       get_a (fn [] (str (.-a this) (.-c this))))
    

    and for the answer to knockout:

    (defvar name_model
        first_name (observable "My")
        last_name (observable "Name")
        name (computed (fn [] (str (. this first_name) " " (. this last_name)))))
    
    (. js/ko (applyBindings name_model));
    

    Which is really nice for me as it matches javascript really well and its entirely readable!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.