Skip to content

Instantly share code, notes, and snippets.

@belozer
Created August 16, 2016 09:42
Show Gist options
  • Save belozer/85e14f3919405b7565522f8e554eaed6 to your computer and use it in GitHub Desktop.
Save belozer/85e14f3919405b7565522f8e554eaed6 to your computer and use it in GitHub Desktop.
Проблема с bemDom.update()
modules.define('app',
['i-bem-dom', 'render', 'store', 'store-router', 'redux-watch'],
function(provide, bemDom, render, store, router, watch) {
provide(bemDom.declBlock(this.name, {
onSetMod: {
js: {
'inited': function() {
setTimeout(() => this.setMod('showed'), 100);
const w = watch(store.getState, 'router.app.handler');
this._unstore = store.subscribe(w(handler => {
this.replaceContent(handler);
}));
},
'': function() {
this._unstore();
}
}
},
replaceContent: function(handler) {
const content = this.findChildBlock('content');
bemDom.replace(content.domElem, render(
{},
{
block: 'content',
mix: [{block: 'app', elem: 'content'}],
mods: {handler: handler}
}
));
}
}));
});
modules.define('page',
['i-bem-dom', 'render', 'store', 'store-router', 'store-auth', 'redux-watch', 'next-tick'],
function(provide, bemDom, render, store, router, auth, reduxWatch, nextTick) {
provide(bemDom.declBlock(this.name, {
onSetMod: {
js: {
inited: function() {
// Так работает
nextTick(() => this._onNextTick());
// Так проблема
// this._onNextTick();
}
}
},
_onNextTick: function() {
this._authCheck();
const w = reduxWatch(store.getState, 'auth.user');
store.subscribe(w(user => {
if (user) {
this.onLogin(user);
} else {
this.onLogout();
}
}));
},
_authCheck: function() {
const state = store.getState();
if (auth.isAuth()) {
this.onLogin(state.auth.user);
} else {
this.onLogout();
}
},
onLogin: function(userData) {
this.delMod('bg-fill');
const handler = router.currentHandler();
bemDom.update(this.domElem, render(
{isAuth: true, user: userData, contentHandler: handler},
{block: 'app'}
));
},
onLogout: function() {
this.setMod('bg-fill');
bemDom.update(this.domElem, render(
{isAuth: false},
{block: 'app'}
));
}
}));
});
modules.require('page', function() {});
@belozer
Copy link
Author

belozer commented Aug 16, 2016

Если делать без некстика в page, то код в app

this._unstore = store.subscribe(w(handler => {
  this.replaceContent(handler);
}));

будет срабатывать 2 раза (в призрачном инстансе и в реальном)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment