Created
April 15, 2013 06:45
-
-
Save thecountofzero/5386173 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steal('can',function(){ | |
Stat = can.Model({ | |
id: "name", | |
findAll: function(){ | |
var def = $.Deferred(); | |
setTimeout(function(){ | |
def.resolve([ | |
{ | |
"id": 0, | |
"name": "Ryan Braun", | |
"homeruns": 2, | |
"atbats": 20, | |
"hits": 12 | |
}, | |
{ | |
"id": 1, | |
"name": "Troy Tulowitzki", | |
"homeruns": 4, | |
"atbats": 20, | |
"hits": 8 | |
} | |
]) | |
},100) | |
return def; | |
} | |
}, { | |
getAverage: function() { | |
return (this.hits / this.atbats).toFixed(3); | |
} | |
}); | |
window.counter = 0; | |
var poll = function () { | |
// Increment the counter | |
counter++; | |
// Fetch stats from server | |
Stat.findAll().then(function(stats) { | |
$("#live").empty(); | |
stats.each(function(s) { | |
s.attr('homeruns', Math.floor(Math.random()*60+1)); | |
}); | |
$("#live").append(can.view('dummyEJS', { | |
stats: stats | |
})); | |
}); | |
// Store the timer ID so we can cancel the polling | |
window.timeoutId = setTimeout(arguments.callee,200); | |
} | |
Stat.findAll().then(function(stats){ | |
$("#live").append(can.view('dummyEJS', { | |
stats: stats | |
})) | |
}) | |
poll(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment