【问题标题】:Rx timer state not updating in view in Cycle.jsRx 计时器状态未在 Cycle.js 中更新
【发布时间】:2016-05-01 15:50:24
【问题描述】:

当有人单击我打算用作某些元素的不透明度的按钮时,我正在启动计时器。当我使用do 跟踪该值时,我可以看到它向控制台吐出 40 次,但在视图中数字保持不变。不知道我哪里错了:

let intent = ({ DOM }) => ({
  clickLogin$: DOM.select('.sign-in').events('click').map(ev => true)
})

let model = ({ clickLogin$ }) =>
  Rx.Observable.combineLatest(
    clickLogin$.startWith(false),
    clickLogin$.map(x =>
      Rx.Observable.timer(1, 1)
    ).switch().startWith(0).take(40),
    (signingIn, fadeValue) => ({ signingIn, fadeValue })
  )

let view = (state$) => {
  return state$.do(
    x => console.log(x.fadeValue)) // this fires |--1-2-3-4-5-6-7-8-->
  .map(({ signingIn, fadeValue }) =>
    div(`.app`, [
      div([fadeValue]), // this value does not change
      If(signingIn,
        div(`.overlay`, {
          style: {
            backgroundColor: `rgba(0, 0, 0, 0.${fadeValue})` // nor does this
          }  
        })
      )
    ])
  )  
}

let main = (sources) => {
  let view$ = view(model(intent(sources)))
  return {
    DOM: view$,
    history: sources.History,
    Props: sources.Props,
  }
}

更新:原来超标中的一个小错误导致它出现奇怪的行为。我什至没有在我的示例中包含它,因为我认为它不相关。

div(`content`, [ `testing` ])

只需将上述更改为(添加类指示)

div(`.content`, [ `testing` ])

让一切都神奇地工作。

【问题讨论】:

  • If(signingIn, If 函数是什么?
  • 另外,您可能想在clickLogin$.map(x => Rx.Observable.timer(1, 1) ).switch().startWith(0).take(40) 的末尾附加一个repeat,因为点击40 次后,observable 将完成,您将不会从中获得更多价值。
  • If 这样一个简单的辅助函数:let If = (c, e) => c ? e : null 但你是对的.. 删除它有效.. 为什么??

标签: javascript rxjs virtual-dom cyclejs hyperscript


【解决方案1】:

这可能不是一个完整的答案,但它有助于识别问题。我删除了视图代码生成的If部分,并添加了repeat,将其放入tricycle,可以看到fadeValue是按预期顺序生成的。

var Cycle = require('@cycle/core');
var CycleDOM = require('@cycle/dom');
var Rx = require('rx');
var makeDOMDriver = CycleDOM.makeDOMDriver;
var div = CycleDOM.div;

var sources = {
  DOM: makeDOMDriver('.app')
};

let main = (sources) => {

let intent = ({ DOM }) => ({
  clickLogin$: Rx.Observable.interval(3000).take(5).share()
})

let model = ({ clickLogin$ }) =>
  Rx.Observable.combineLatest(
    clickLogin$.startWith(false),
    clickLogin$.flatMapLatest(function (x) {
      return Rx.Observable.timer(200, 200);
    }).take(10).repeat(),
    (signingIn, fadeValue) => ({ signingIn, fadeValue })
  )

let view = (state$) => {
  return state$.do(
    x => console.log(x.fadeValue)) // this fires |--1-2-3-4-5-6-7-8-->
  .map(({ signingIn, fadeValue }) =>
    div(`.app`, [
      div([fadeValue]) // this value does not change
    ])
  )  
}

  let view$ = view(model(intent(sources)))
  return {
    DOM: view$,
    history: sources.History,
    Props: sources.Props,
  }
}

【讨论】:

  • 嘿,谢谢!你确实帮我找到了答案....这是因为我在超标标签中缺少...div('content', ['testing'])。没有类指示器的内容使一切都无声无息地爆炸。
  • 感谢@azium!不,谢谢,@cycle/dom(或虚拟超脚本)中令人惊讶的静默失败!呃
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
相关资源
最近更新 更多