Skip to content

Commit

Permalink
Merge pull request #20 from 1000hz/feature/add_createActions_shorthand
Browse files Browse the repository at this point in the history
Adding a shorthand form of alt.createActions()
  • Loading branch information
goatslacker committed Feb 23, 2015
2 parents aff1807 + cf0ff0b commit 84e6bc4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ var locationActions = alt.createActions(LocationActions)
locationActions.updateLocation('South Lake Tahoe', 'California')
```

There's even a shorthand for the shorthand if all you're doing is generating a list of actions

```js
var locationActions = alt.generateActions('updateLocation', 'updateCity', 'updateState', 'updateCountry')
```

### Stores

Stores are where you keep a part of your application's state. It's a singleton, holds your data, and is immutable.
Expand Down
34 changes: 26 additions & 8 deletions dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"use strict";

var Dispatcher = require("flux").Dispatcher;
var EventEmitter = babelHelpers.interopRequire(require("eventemitter3"));

var EventEmitter = babelHelpers.interopRequire(require("eventemitter3"));
var Symbol = babelHelpers.interopRequire(require("es-symbol"));

var assign = babelHelpers.interopRequire(require("object-assign"));

var now = Date.now();
Expand Down Expand Up @@ -48,6 +47,7 @@ var getInternalMethods = function (obj, excluded) {
var AltStore = (function () {
function AltStore(dispatcher, state) {
var _this5 = this;

babelHelpers.classCallCheck(this, AltStore);

this[EE] = new EventEmitter();
Expand Down Expand Up @@ -100,7 +100,6 @@ var AltStore = (function () {
configurable: true
}
});

return AltStore;
})();

Expand All @@ -123,7 +122,6 @@ var ActionCreator = (function () {
configurable: true
}
});

return ActionCreator;
})();

Expand Down Expand Up @@ -154,6 +152,7 @@ var StoreMixin = {

bindActions: function bindActions(actions) {
var _this5 = this;

Object.keys(actions).forEach(function (action) {
var symbol = actions[action];
var matchFirstCharacter = /./;
Expand Down Expand Up @@ -247,10 +246,12 @@ var Alt = (function () {
createStore: {
value: function createStore(StoreModel, iden) {
var _this5 = this;

var key = iden || StoreModel.displayName || StoreModel.name;
// Creating a class here so we don't overload the provided store's
// prototype with the mixin behaviour and I'm extending from StoreModel
// so we can inherit any extensions from the provided store.

var Store = (function (StoreModel) {
function Store() {
babelHelpers.classCallCheck(this, Store);
Expand All @@ -261,7 +262,6 @@ var Alt = (function () {
}

babelHelpers.inherits(Store, StoreModel);

return Store;
})(StoreModel);

Expand Down Expand Up @@ -289,10 +289,31 @@ var Alt = (function () {
writable: true,
configurable: true
},
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}

var ActionsClass = function ActionsClass() {
var _ref;

babelHelpers.classCallCheck(this, ActionsClass);

(_ref = this).generateActions.apply(_ref, actionNames);
};

return this.createActions(ActionsClass);
},
writable: true,
configurable: true
},
createActions: {
value: function createActions(ActionsClass) {
var _this5 = this;

var exportObj = arguments[1] === undefined ? {} : arguments[1];

var actions = assign({}, getInternalMethods(ActionsClass.prototype, builtInProto));
var key = ActionsClass.displayName || ActionsClass.name;

Expand All @@ -304,7 +325,6 @@ var Alt = (function () {
}

babelHelpers.inherits(ActionsGenerator, ActionsClass);

babelHelpers.prototypeProperties(ActionsGenerator, null, {
generateActions: {
value: function generateActions() {
Expand All @@ -327,7 +347,6 @@ var Alt = (function () {
configurable: true
}
});

return ActionsGenerator;
})(ActionsClass);

Expand Down Expand Up @@ -449,7 +468,6 @@ var Alt = (function () {
configurable: true
}
});

return Alt;
})();

Expand Down
26 changes: 26 additions & 0 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (st
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };

var Dispatcher = require("flux").Dispatcher;

var EventEmitter = _interopRequire(require("eventemitter3"));

var Symbol = _interopRequire(require("es-symbol"));
Expand Down Expand Up @@ -58,6 +59,7 @@ var getInternalMethods = function (obj, excluded) {
var AltStore = (function () {
function AltStore(dispatcher, state) {
var _this5 = this;

_classCallCheck(this, AltStore);

this[EE] = new EventEmitter();
Expand Down Expand Up @@ -164,6 +166,7 @@ var StoreMixin = {

bindActions: function bindActions(actions) {
var _this5 = this;

Object.keys(actions).forEach(function (action) {
var symbol = actions[action];
var matchFirstCharacter = /./;
Expand Down Expand Up @@ -257,10 +260,12 @@ var Alt = (function () {
createStore: {
value: function createStore(StoreModel, iden) {
var _this5 = this;

var key = iden || StoreModel.displayName || StoreModel.name;
// Creating a class here so we don't overload the provided store's
// prototype with the mixin behaviour and I'm extending from StoreModel
// so we can inherit any extensions from the provided store.

var Store = (function (StoreModel) {
function Store() {
_classCallCheck(this, Store);
Expand Down Expand Up @@ -299,10 +304,31 @@ var Alt = (function () {
writable: true,
configurable: true
},
generateActions: {
value: function generateActions() {
for (var _len = arguments.length, actionNames = Array(_len), _key = 0; _key < _len; _key++) {
actionNames[_key] = arguments[_key];
}

var ActionsClass = function ActionsClass() {
var _ref;

_classCallCheck(this, ActionsClass);

(_ref = this).generateActions.apply(_ref, actionNames);
};

return this.createActions(ActionsClass);
},
writable: true,
configurable: true
},
createActions: {
value: function createActions(ActionsClass) {
var _this5 = this;

var exportObj = arguments[1] === undefined ? {} : arguments[1];

var actions = assign({}, getInternalMethods(ActionsClass.prototype, builtInProto));
var key = ActionsClass.displayName || ActionsClass.name;

Expand Down
10 changes: 10 additions & 0 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ your own custom identifier for each store`
return this.stores[key]
}

generateActions(...actionNames) {
class ActionsClass {
constructor() {
this.generateActions(...actionNames)
}
}

return this.createActions(ActionsClass)
}

createActions(ActionsClass, exportObj = {}) {
let actions = assign(
{},
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class MyActions {
let myActions = {}
alt.createActions(MyActions, myActions)

let myShorthandActions = alt.generateActions("actionOne", "actionTwo")

class MyStore {
constructor() {
let myActionsInst = this.alt.getActions('myActions')
Expand Down Expand Up @@ -300,6 +302,8 @@ let tests = {
assert.equal(typeof myActions.updateTwo, 'function', 'prototype defined actions exist')
assert.equal(typeof myActions.updateThree, 'function', 'prototype defined actions exist')
assert.equal(myActions.updateTwo.length, 2, 'actions can have > 1 arity')
assert.equal(typeof myShorthandActions.actionOne, 'function', 'action created with shorthand createActions exists')
assert.equal(typeof myShorthandActions.actionTwo, 'function', 'other action created with shorthand createActions exists')
},

'existence of constants'() {
Expand Down

0 comments on commit 84e6bc4

Please sign in to comment.