Пытаюсь обернуть Knockout.js в clojurescript, но получается очень сложно. Проблема, с которой я сталкиваюсь, - это ссылка на переменную this. Я думаю сдаться и просто использовать javascript напрямую.
Я взял примеры с http://knockoutjs.com/examples/helloWorld.htmlи http://knockoutjs.com/examples/contactsEditor.html
Мне удалось обернуть простые функции некоторыми макросами. Например:
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);
};
становится:
(defviewmodel data
(observing :first_name "Bert")
(observing :last_name "Bertington")
(computing :name [:first_name :last_name]
(str :first_name " " :last_name)))
Однако для чего-то более сложного, например:
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());
Я не уверен, что я могу сделать в clojurescript, чтобы сопоставить такой код: this.allItems.push(this.itemToAdd ())
Есть мысли?