Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue
Last active August 29, 2015 14:22
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 BerkeleyTrue/5a1f64eae5a3502622b4 to your computer and use it in GitHub Desktop.
Save BerkeleyTrue/5a1f64eae5a3502622b4 to your computer and use it in GitHub Desktop.
Thundercats es5 class creation
// current es6/7 method
import { Actions } from 'thundercats';
export default class CatActions extends Actions {
constructor() {
super([
'clickThread'
]);
}
static displayName = 'CatActions'
doSomething(meow) {
return { meow };
}
}
// useing es5 prototype
var Actions = require('thundercats').Actions;
var _ = require('lodash');
function CatActions() {
Actions.call(this, ['clickThread']);
}
CatActions.displayName = 'CatActions';
CatActions.prototype.constructor = Actions;
CatActions.prototype = new Actions();
_.assign(CatActions.prototype, {
doActions: function(meow) {
return { meow: meow };
}
});
// es6/7
import { Store } from 'thundercats';
export default class TodoStore extends Store {
constructor(cat) { // We are passing an instance of the Cat into the constructor
super();
const todoActions = cat.getActions('todoActions');
const { create } = todoActions;
// set the initial value of the store
this.value = {
todosMap: {}
};
// create is an RxJS observable, so you can use any observable operation available to you in RxJS.
const createTodoObs = create.map(({ todo, promise }) => {
// The store expects observables to return objects with specific keys. `replace`, `set`, `transform`, `optimistic`
// Here is an example using `transform` and `optimistic`
return {
transform: function (state) {
const todos = Object.assign({}, state.todosMap);
todos[todo.id] = todo;
state.todosMap = todos;
return state;
},
optimistic: promise
};
});
// You register observables for the store to listen too. Any observable can be registered.
this.register(createTodoObs);
}
static displayName = 'TodoStore'
}
// es5
var Store = require('thundercats').Store;
var _ = require('lodash');
Function TodoStore(cat) {
Store.call(this);
var todoActions = cat.getActions('todoActions');
var create = todoActions.create;
// set the initial value of the store
this.value = {
todosMap: {}
};
// create is an RxJS observable, so you can use any observable operation available to you in RxJS.
var createTodoObs = create.map(function(dats) {
var todo = dats.todo, promise = dats.promise;
// The store expects observables to return objects with specific keys. `replace`, `set`, `transform`, `optimistic`
// Here is an example using `transform` and `optimistic`
return {
transform: function (state) {
var todos = {};
_.assign(todos, state.todosMap);
todos[todo.id] = todo;
state.todosMap = todos;
return state;
},
optimistic: promise
};
});
// You register observables for the store to listen too. Any observable can be registered.
this.register(createTodoObs);
}
TodoStore.displayName = 'TodoStore';
TodoStore.prototype = new Store();
TodoStore.constructor = Store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment