Add state() method handling for components

pull/1028/head
Emil Ajdyna 7 years ago
parent 7d949a601c
commit d398b34a41

@ -610,6 +610,10 @@ export default class Generator {
addDeclaration('setup', templateProperties.setup.value);
}
if (templateProperties.store) {
addDeclaration('store', templateProperties.store.value);
}
if (templateProperties.tag) {
this.tag = templateProperties.tag.value.value;
}

@ -181,8 +181,7 @@ export default function dom(
// generate initial state object
const expectedProperties = Array.from(generator.expectedProperties);
const globals = expectedProperties.filter(prop => globalWhitelist.has(prop));
const storeProps = options.store ? expectedProperties.filter(prop => prop[0] === '$') : [];
const storeProps = options.store || templateProperties.store ? expectedProperties.filter(prop => prop[0] === '$') : [];
const initialState = [];
if (globals.length > 0) {
@ -206,6 +205,7 @@ export default function dom(
${options.dev && !generator.customElement &&
`if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option");`}
@init(this, options);
${templateProperties.store && `this.store = %store();`}
${generator.usesRefs && `this.refs = {};`}
this._state = @assign(${initialState.join(', ')});
${storeProps.length > 0 && `this.store._add(this, [${storeProps.map(prop => `"${prop.slice(1)}"`)}]);`}

@ -72,7 +72,7 @@ export default function ssr(
// generate initial state object
const expectedProperties = Array.from(generator.expectedProperties);
const globals = expectedProperties.filter(prop => globalWhitelist.has(prop));
const storeProps = options.store ? expectedProperties.filter(prop => prop[0] === '$') : [];
const storeProps = options.store || templateProperties.store ? expectedProperties.filter(prop => prop[0] === '$') : [];
const initialState = [];
if (globals.length > 0) {
@ -80,7 +80,12 @@ export default function ssr(
}
if (storeProps.length > 0) {
initialState.push(`options.store._init([${storeProps.map(prop => `"${prop.slice(1)}"`)}])`);
const initialize = `_init([${storeProps.map(prop => `"${prop.slice(1)}"`)}])`
if (options.store) {
initialState.push(`options.store.${initialize}`);
} else if (templateProperties.store) {
initialState.push(`%store().${initialize}`);
}
}
if (templateProperties.data) {

@ -71,7 +71,7 @@ export function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
export function observe(key, callback, options) {

@ -14,6 +14,7 @@ import props from './props';
import tag from './tag';
import transitions from './transitions';
import setup from './setup';
import store from './store';
export default {
data,
@ -32,4 +33,5 @@ export default {
tag,
transitions,
setup,
store,
};

@ -0,0 +1,6 @@
import { Validator } from '../../';
import { Node } from '../../../interfaces';
export default function store(validator: Validator, prop: Node) {
// not sure there's anything we need to check here...
}

@ -97,7 +97,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -73,7 +73,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -73,7 +73,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -93,7 +93,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -85,7 +85,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -89,7 +89,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -93,7 +93,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -93,7 +93,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -105,7 +105,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -85,7 +85,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -89,7 +89,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -89,7 +89,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -89,7 +89,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -89,7 +89,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -89,7 +89,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -89,7 +89,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -93,7 +93,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -91,7 +91,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -108,7 +108,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -101,7 +101,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -87,7 +87,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -73,7 +73,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -73,7 +73,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -97,7 +97,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -93,7 +93,7 @@ function init(component, options) {
component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}
function observe(key, callback, options) {

@ -0,0 +1,9 @@
export default {
html: `<h1>Hello world!</h1>`,
test(assert, component, target) {
component.store.set({ name: 'everybody' });
assert.htmlEqual(target.innerHTML, `<h1>Hello everybody!</h1>`);
}
};

@ -0,0 +1,11 @@
<h1>Hello {{$name}}!</h1>
<script>
import { Store } from '../../../../store.js';
export default {
store () {
return new Store({
name: 'world'
});
}
}
</script>
Loading…
Cancel
Save