Skip to content

Instantly share code, notes, and snippets.

@publickeating
Last active August 29, 2015 13:55
Show Gist options
  • Save publickeating/8697626 to your computer and use it in GitHub Desktop.
Save publickeating/8697626 to your computer and use it in GitHub Desktop.
MyApp.SectionState = SC.State.extend({
initialSubstate: 'noCategory',
noCategory: SC.State.extend({
representRoute: 'sections/:section_id',
enterState: function (context) {
var section = context.section;
// Set the location.
this.set('location', 'section/' + section.get('id'));
}
}),
category: SC.State.plugin('MyApp.CategoryState'),
enterStateByRoute: function (routeContext) {
var sectionId = routeContext.params.section_id,
section = SL.store.find(SL.NGASection, sectionId);
}
});
MyApp.CategoryState = SC.State.extend({
representRoute: 'sections/:section_id/categories/:category_id',
/** @private */
enterState: function (context) {
var category = context.category;
// Set the location.
this.set('location', 'categories/' + category.get('id'));
return SC.Async.perform('_enterStateCommon', category);
},
/** @private */
enterStateByRoute: function (routeContext) {
var categoryId = routeContext.params.category_id,
category = MyApp.store.find(MyApp.Category, categoryId);
return SC.Async.perform('_enterStateCommon', category);
},
/** @private */
_enterStateCommon: function (category) {
// Update the title.
document.title = "Category | " + category.get('name');
// Select the category.
MyApp.categoriesController.selectObject(category);
// Keep transitioning (after bindings flush).
this.invokeLast(function () {
this.resumeGotoState();
});
}
});
SC.State.reopen({
stateCanBeEntered: function (context) {
return true;
}
});
MyApp.statechart.mixin({
_blockedSubstate: null,
canGoToState: function (state, context) {
var substates,
ret;
state = this.getState(state);
substates = this._createStateChain(state);
for (var i = substates.length - 1; i >= 0; i--) {
var substate = substates[i];
ret = substate.stateCanBeEntered(context);
if (!ret) {
this.set('_blockedSubstate', substate);
break;
} // Stop as soon as we can't enter any one state.
}
return ret;
},
recoverForEntry: function (state, context) {
var blockedSubstate = this._blockedSubstate,
ret;
if (blockedSubstate && blockedSubstate.recoverForEntry) {
ret = blockedSubstate.recoverForEntry(context);
// Clear the cached value.
this._blockedSubstate = null;
} else {
ret = false;
}
return SC.none(ret) || ret;
}
});
MyApp.statechartController = SC.Object.create(SC.StatechartDelegate, {
/** @private */
_queuedLocation: null,
// ------------------------------------------------------------------------
// Methods
//
continueRouting: function () {
var queuedLocation = this._queuedLocation;
// console.warn('continueRouting(): %@'.fmt(queuedLocation));
this._queuedLocation = null;
if (!SC.none(queuedLocation)) {
SC.routes.trigger();
return true;
}
// Clear out the queued location.
return false;
},
statechartShouldStateHandleTriggeredRoute: function (statechart, state, context) {
console.warn('statechartShouldStateHandleTriggeredRoute(.., %@, %@)'.fmt(state, context.location));
return statechart.canGoToState(state, context);
},
statechartStateCancelledHandlingTriggeredRoute: function (statechart, state, context) {
console.warn('statechartStateCancelledHandlingTriggeredRoute(.., %@, %@)'.fmt(state, context.location));
if (statechart.recoverForEntry(state, context)) {
this._queuedLocation = context.location;
}
}
});
// Assign as delegate.
MyApp.statechart.set('delegate', MyApp.statechartController);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment