Permalink
Browse files

Adds basic application and statechart structure

  • Loading branch information...
1 parent d954f9d commit bd6571e4bfdcaeafd0e35dd9cea3b4ddff9b69a9 Dominik Guzei committed Jan 31, 2012
View
@@ -14,3 +14,4 @@
/log/*.log
/tmp
.idea
+.DS_Store
@@ -0,0 +1,19 @@
+
+App = SC.Application.create {
+
+ ready: ->
+ App.statechart.initStatechart();
+ @_super()
+
+}
+
+# create namespaces
+App.models = {}
+App.views = {}
+App.controllers = {}
+App.mediators = {}
+App.states = {}
+App.templates = {}
+
+# export App to global namespace
+this.App = App
@@ -0,0 +1,26 @@
+
+App = this.App
+
+App.controllers.statechartController = SC.Object.create(SC.StatechartDelegate, {
+
+ lastRouteContext: null
+
+ statechartShouldStateHandleTriggeredRoute: (statechart, state, context) ->
+ @set 'lastRouteContext', context
+
+ lastCheckedState = state
+ shouldTrigger = true
+
+ while shouldTrigger and lastCheckedState?
+
+ if lastCheckedState.beforeFilter
+ shouldTrigger = lastCheckedState.beforeFilter()
+ if not shouldTrigger
+ SC.routes.set 'location', null
+ break
+
+ lastCheckedState = lastCheckedState.get 'parentState'
+
+ return shouldTrigger
+
+})
@@ -0,0 +1,11 @@
+
+App = this.App
+
+App.mediators.authenticationMediator = SC.Object.create {
+
+ userName: null,
+ password: null,
+
+ loggedIn: NO
+
+}
@@ -0,0 +1,19 @@
+
+#= require app/utils/stateToString
+#= require app/utils/statechartAsActionTarget
+#= require app/controllers/statechartController
+#= require app/states/BrowsingState
+#= require app/states/AuthenticationState
+
+App = this.App
+
+App.statechart = SC.Statechart.create {
+
+ statesAreConcurrent: YES
+ trace: YES
+
+ delegate: App.controllers.statechartController
+
+ Browsing: App.states.BrowsingState
+ Authentication: App.states.AuthenticationState
+}
@@ -0,0 +1,58 @@
+
+#= require app/mediators/authenticationMediator
+
+App = this.App
+
+App.states.AuthenticationState = SC.State.extend {
+
+ initialSubstate: 'Login',
+
+ Login: SC.State.extend {
+
+ initialSubstate: 'LoggedOut'
+
+ LoggedOut: SC.State.extend {
+
+ enterState: ->
+ App.mediators.authenticationMediator.set 'loggedIn', NO
+ App.mediators.authenticationMediator.set 'userName', null
+ App.mediators.authenticationMediator.set 'password', null
+
+ login: ->
+ @gotoState 'Authentication.Login.Authenticate'
+ }
+
+ LoggedIn: SC.State.extend {
+ enterState: ->
+ App.mediators.authenticationMediator.set 'loggedIn', YES
+
+ context = App.controllers.statechartController.get 'lastRouteContext'
+ if context then context.retry()
+
+ logout: ->
+ @gotoState 'Authentication.Login.LoggedOut'
+ }
+
+ Authenticate: SC.State.extend {
+ enterState: ->
+ App.mediators.authenticationMediator.set 'userName', 'DominikGuzei'
+ App.mediators.authenticationMediator.set 'password', 'xyz'
+
+ @gotoState 'Authentication.Login.LoggedIn'
+ }
+ }
+
+ Registration: SC.State.extend {
+
+ initialSubstate: 'Intern'
+
+ Intern: SC.State.extend {
+
+ }
+
+ Facebook: SC.State.extend {
+
+ }
+ }
+
+}
@@ -0,0 +1,41 @@
+#= require app/mediators/authenticationMediator
+
+App = this.App
+
+App.states.BrowsingState = SC.State.extend {
+
+ initialSubstate: 'Public'
+
+ Public: SC.State.extend {
+
+ initialSubstate: 'ListNotes'
+
+ ListNotes: SC.State.extend {
+
+ representRoute: 'notes'
+
+ createNote: ->
+ @gotoState 'Browsing.Restricted.CreateNote'
+ }
+ }
+
+ Restricted: SC.State.extend {
+
+ initialSubstate: 'CreateNote'
+
+ beforeFilter: (context) ->
+ unless App.mediators.authenticationMediator.get('loggedIn')
+ @get('statechart').send('login')
+ return false
+
+ return true
+
+ CreateNote: SC.State.extend {
+ representRoute: 'notes/create'
+ }
+
+ logout: ->
+ @gotoState 'Browsing.Public.ListNotes'
+ }
+
+}
@@ -0,0 +1,5 @@
+
+SC.State.reopen {
+ toString: ->
+ return @get 'fullPath'
+}
@@ -0,0 +1,5 @@
+
+SC.Statechart.reopen {
+ send: ->
+ @sendAction.apply(this, arguments)
+}
@@ -12,4 +12,9 @@
//
//= require jquery
//= require jquery_ujs
-//= require_tree .
+//= require lib/ember
+//= require lib/sproutcore-routing
+//= require lib/ember-statechart
+
+//= require app/App
+//= require app/statechart
Oops, something went wrong.

0 comments on commit bd6571e

Please sign in to comment.