Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Created December 6, 2011 01:43
Show Gist options
  • Save ccapndave/1436310 to your computer and use it in GitHub Desktop.
Save ccapndave/1436310 to your computer and use it in GitHub Desktop.
Tictactoe SC2
App = SC.Application.create()
### Model ###
class App.Game extends SC.Object
@createNewGame: ->
game = App.Game.create( { cells: [] } )
for x in [0...3]
for y in [0...3]
game.get("cells").pushObject App.Cell.create { x, y }
game
class App.Cell extends SC.Object
### Controller ###
### View ###
App.HeaderView = SC.View.extend
headerText: "Hello"
###
App.BoardView = SC.ContainerView.extend
classNames: "board"
init: ->
@_super()
for cell in App.currentGame.cells
cellView = App.CellView.create( { content: cell } )
@get("childViews").push @createChildView(cellView)
###
App.BoardView = SC.CollectionView.extend
contentBinding: "App.currentGame.cells"
exampleView: App.CellView
App.CellView = SC.View.extend
classNames: "cell".w()
classNameBindings: [ "horizontalClass", "verticalClass" ]
horizontalClass: (->
switch @content.get("x")
when 0 then "left"
when 1 then "middle"
when 2 then "right"
).property("content")
verticalClass: (->
switch @content.get("y")
when 0 then "top"
when 1 then "middle"
when 2 then "bottom"
).property("content")
### Initialize ###
App.currentGame = App.Game.createNewGame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment