Skip to content

Instantly share code, notes, and snippets.

@stones
Created July 29, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stones/2ea290680c21045711ba to your computer and use it in GitHub Desktop.
Save stones/2ea290680c21045711ba to your computer and use it in GitHub Desktop.
DeLorean Tutorial
document.getElementById('addItem').onclick = function () {
ActionCreator.addItem();
};
var ActionCreator = {
addItem: function () {
// We'll going to call dispatcher methods.
MyAppDispatcher.addItem({random: Math.random()});
}
};
var MyAppDispatcher = DeLorean.Flux.createDispatcher({
addItem: function (data) {
this.dispatch('addItem', data);
},
getStores: function () {
return {
myStore: myStore
}
}
});
var MyAppStore = DeLorean.Flux.createStore({
scheme: {
list: {
default: [],
calculate: function (value) {
return value.concat().map(function (item) {
return 'ITEM: ' + item;
});
}
}
},
actions: {
// Remember the `dispatch('addItem')`
'addItem': 'addItemMethod'
},
addItemMethod: function (data) {
// It automatically emits change event.
this.set('list', this.list.concat(data.random));
}
});
var myStore = new MyAppStore();
var list = document.getElementById('list');
// Store emits the `change` event when changed.
myStore.onChange(function () {
list.innerHTML = ''; // Best thing for this example.
myStore.store.list.forEach(function (item) {
var listItem = document.createElement('li');
listItem.innerHTML = item;
list.appendChild(listItem);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment