Commit 49ca24e3 by Иван Кубота

store set\get nested key. wip

parent c0551e88
......@@ -10,7 +10,8 @@
"yargs": "^15.1.0"
},
"scripts": {
"start": "node index.js"
"start": "node index.js",
"test": "mocha"
},
"bin": {
"kus-quizard": "./bin/index.js"
......@@ -19,6 +20,8 @@
"@babel/core": "^7.8.3",
"@babel/plugin-transform-react-jsx": "^7.8.3",
"@babel/template": "^7.8.3",
"babel-plugin-module-resolver": "^4.0.0"
"babel-plugin-module-resolver": "^4.0.0",
"chai": "^4.2.0",
"mocha": "^7.0.1"
}
}
......@@ -29,3 +29,4 @@ Observable.prototype = {
}
}
};
typeof module === 'object' && (module.exports = Observable);
\ No newline at end of file
......@@ -9,16 +9,53 @@ const Store = function(cfg) {
};
Store.prototype = {
set: function(key, val) {
if(this._props[key] !== val){
this._props[ key ] = val;
_set: function(keys, val, pointer, path){
let i, _i;
let key = keys[path.length];
if(pointer[key] !== val){
// TODO make it work
if(typeof val === 'object' && key in pointer){
this._set(keys, val[key], pointer[key], path.concat(key));
}
pointer[ key ] = val;
this.fire( 'change', key, val );
this.fire( key, val );
}
},
get: function(key) {
return this._props[key];
set: function(key, val) {
return this._set(
typeof key === 'string' ? key.split('.') : key,
val,
this._props,
[]
);
},
get: function(key, returnLastStore) {
key = typeof key === 'string' ? key.split('.') : key;
let ref = this,
lastStore = ref, i, _i;
for( i = 0, _i = key.length; i < _i; i++ ){
if( ref instanceof Store ){
ref = ref._props[key[i]];
lastStore = ref;
}else{
ref = ref[key[i]];
}
if( !ref && i < key.length - 1 )
return void 0;
}
if( returnLastStore )
return { lastQObject: lastStore, result: ref };
return ref;
},
sub: function(key, fn) {
if(Array.isArray(key)){
var wrap = () => fn.apply(this, key.map(key=>this.get(key)));
......@@ -56,3 +93,5 @@ StoreBinding.prototype = {
}
};
Store.prototype = Object.assign(new Observable, Store.prototype);
typeof module === 'object' && (module.exports = Store);
\ No newline at end of file
const store = new Store({
'navigation.current': 'login'
'navigation': {
current: 'login'
},
'account': {
name: 'Диогроген Курославович',
phone: '79999877414'
}
});
......
......@@ -2,6 +2,15 @@ import './Header.scss';
export default D.declare('view.block.Header', () => {
return <header class="page-header">
{
[
'login',
'main'
].map(name=>
<button type={'button'} onClick={() => store.set('navigation.current', name)}>{name}</button>
)
}
</header>
});
......@@ -2,7 +2,8 @@
export default D.declare('view.page.Account', ()=>
<div>
Account Page
<button type={'button'} onClick={() => store.set('navigation.current', 'login')}>To login</button>
<div>
Hello, {_=>store.sub('account', account=> _(account.name))}
</div>
</div>
)
......@@ -30,6 +30,5 @@ export default D.declare('view.page.Login', () => {
]
})}
<button type={'button'} onclick={() => store.set('navigation.current', 'main')}>To account</button>
</div>
})
const Observable = global.Observable = require('../src/core/Observer');
const Store = require('../src/core/data/store/Store');
const assert = require('chai').assert;
describe('store', function() {
it('should set and get simple values', function() {
const s = new Store({});
s.set('a', 5);
assert.equal(s.get('a'), 5);
s.set('b', 'abc');
assert.equal(s.get('b'), 'abc');
assert.equal(s.get('a'), 5);
s.set('c', [1,2,3]);
assert.deepEqual(s.get('c'), [1,2,3]);
});
it('should set complex values', function () {
const s = new Store({});
s.set('a.b.c', 33);
assert.equal(s.get('a.b.c'), 33);
s.set('a.b', {c:12, d: 20});
assert.equal(s.get('a.b.c'), 12);
assert.equal(s.get('a.b.d'), 20);
s.set('a.b', null);
assert.equal(s.get('a.b.d'), void 0);
})
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment