New boilerplate
This commit is contained in:
@@ -1,63 +1,60 @@
|
||||
var React = require('flux-react');
|
||||
var merge = require('react/lib/merge');
|
||||
var CheckboxActions = require('../actions/CheckboxActions.js');
|
||||
var mergeInto = require('react/lib/mergeInto');
|
||||
var Constants = require('../Constants.js');
|
||||
var flux = require('flux-react');
|
||||
var actions = require('./../actions.js');
|
||||
|
||||
var checkboxes = [{
|
||||
color: 'red',
|
||||
checked: false
|
||||
}, {
|
||||
color: 'blue',
|
||||
checked: false
|
||||
}, {
|
||||
color: 'green',
|
||||
checked: false
|
||||
}];
|
||||
|
||||
var CheckboxStore = React.createStore({
|
||||
|
||||
getCheckboxes: function () {
|
||||
return checkboxes.map(function (checkbox) {
|
||||
var checkboxCopy = {};
|
||||
mergeInto(checkboxCopy, checkbox); // Create a copy to make it impossible to mutate store
|
||||
return checkboxCopy;
|
||||
});
|
||||
var CheckboxStore = flux.createStore({
|
||||
getInitialState: function () {
|
||||
return {
|
||||
checkboxes: [{
|
||||
color: 'red',
|
||||
checked: false
|
||||
}, {
|
||||
color: 'blue',
|
||||
checked: false
|
||||
}, {
|
||||
color: 'green',
|
||||
checked: false
|
||||
}]
|
||||
};
|
||||
},
|
||||
getColors: function () {
|
||||
return checkboxes.map(function (checkbox) {
|
||||
if (checkbox.checked) {
|
||||
return checkbox.color;
|
||||
} else {
|
||||
return 'black';
|
||||
actions: [
|
||||
actions.checkAll,
|
||||
actions.uncheckAll,
|
||||
actions.check
|
||||
],
|
||||
checkAll: function () {
|
||||
this.state.checkboxes.forEach(function (checkbox) {
|
||||
checkbox.checked = true;
|
||||
});
|
||||
this.emitChange();
|
||||
},
|
||||
uncheckAll: function () {
|
||||
this.state.checkboxes.forEach(function (checkbox) {
|
||||
checkbox.checked = false;
|
||||
});
|
||||
this.emitChange();
|
||||
},
|
||||
check: function (color) {
|
||||
this.state.checkboxes.forEach(function (checkbox) {
|
||||
if (checkbox.color === color) {
|
||||
checkbox.checked = !checkbox.checked;
|
||||
}
|
||||
});
|
||||
this.emitChange();
|
||||
},
|
||||
dispatch: function (payload) {
|
||||
switch (payload.type) {
|
||||
case CheckboxActions.CHECK_ALL:
|
||||
checkboxes.forEach(function (checkbox) {
|
||||
checkbox.checked = true;
|
||||
});
|
||||
break;
|
||||
|
||||
case CheckboxActions.UNCHECK_ALL:
|
||||
checkboxes.forEach(function (checkbox) {
|
||||
checkbox.checked = false;
|
||||
});
|
||||
break;
|
||||
|
||||
case CheckboxActions.CHECK:
|
||||
checkboxes.forEach(function (checkbox) {
|
||||
if (checkbox.color === payload.color) {
|
||||
checkbox.checked = !checkbox.checked;
|
||||
exports: {
|
||||
getCheckboxes: function () {
|
||||
return this.checkboxes;
|
||||
},
|
||||
getColors: function () {
|
||||
return this.checkboxes.map(function (checkbox) {
|
||||
if (checkbox.checked) {
|
||||
return checkbox.color;
|
||||
} else {
|
||||
return 'black';
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
this.flush();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
module.exports = CheckboxStore;
|
||||
Reference in New Issue
Block a user