Note that there are some explanatory texts on larger screens.

plurals
  1. POwrapping knockout.js using clojurescript
    text
    copied!<p>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.</p> <p>I've taken examples off <a href="http://knockoutjs.com/examples/helloWorld.html" rel="nofollow">http://knockoutjs.com/examples/helloWorld.html</a> and <a href="http://knockoutjs.com/examples/contactsEditor.html" rel="nofollow">http://knockoutjs.com/examples/contactsEditor.html</a></p> <p>I've managed to wrap easy functions with some macros. For example:</p> <pre><code>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); }; </code></pre> <p>becomes:</p> <pre><code>(defviewmodel data (observing :first_name "Bert") (observing :last_name "Bertington") (computing :name [:first_name :last_name] (str :first_name " " :last_name))) </code></pre> <p>However, for something harder like:</p> <pre><code>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() != "") &amp;&amp; (this.allItems.indexOf(this.itemToAdd()) &lt; 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()); </code></pre> <p>I'm not sure what I can do in clojurescript to match code like this: <code>this.allItems.push(this.itemToAdd())</code></p> <p>Any thoughts?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload