2
我試着翻譯下面的代碼的CoffeeScript作爲一種實踐canoical方式計算正確的觀察到在CoffeeScript中
function StateVM() {
var self = this;
self.state = ko.observable("stopped");
self.counter = ko.observable(0);
self.timeStopped = ko.observable(new Date());
self.timeStarted = ko.observable(new Date());
self.currentTime = ko.observable(new Date());
self.timeRunning = ko.computed(function(){
var num = (self.currentTime() - self.timeStarted())/1000 >> 0;
return (num < 0) ? 0 : num;
});
self.isStopped = ko.computed(function(){
return (self.state() === "stopped");
});
self.isStarted = ko.computed(function(){
return (self.state() === "running");
});
$(document).ready(function(){
var vm = new StateVM();
ko.applyBindings(vm);
});
現在我在這的CoffeeScript代碼,但我正在與問題自referentials @
編譯進錯範圍:
obs = ko.observable
cmp = ko.computed
class StateVM
constructor: ->
state = obs "stopped"
counter = obs 0
timeStopped = obs new Date
timeStarted = obs new Date
currentTime = obs new Date
timeRunning = cmp ->
x = (@currentTime() - @timeStarted())/1000 >> 0
x < 0 ? 0 : x
isStopped = cmp -> @state() == "stopped"
isStarted = cmp -> @state() == "running"
$ -> ko.applyBindings(new StateVM)
是否有這樣做的一個典型方式是什麼?
豎起大拇指的問題和答案! –