首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法让Riot.js observable通过回调传递参数

无法让Riot.js observable通过回调传递参数
EN

Stack Overflow用户
提问于 2017-02-14 00:17:50
回答 1查看 430关注 0票数 0

我的Riot.js observable的工作方式如下。

在我的' global -header‘标签中,riot全局作用域中的riot.store observable被更新(用户在输入字段中输入一个新的高度),然后observable触发器'update_dims':

代码语言:javascript
复制
save_height() {
  riot.store.cardHeight = this.refs.input_card_height.value
  riot.store.trigger('update_dims')
}

在我的‘riot.store’标签中,卡片监听'update_dims‘并成功地更新接口中的{ myCardHeight }。

代码语言:javascript
复制
// function updates the card height
update_cardHeight () {
  this.myCardHeight = riot.store.cardHeight
  this.update()
}
// observable runs when triggered and calls above function 'update_cardHeight'
riot.store.on('update_dims',this.update_cardDimensions)

但是,如果我尝试直接从riot.store.trigger传递参数:

代码语言:javascript
复制
save_height() {
  riot.store.cardHeight = this.refs.input_card_height.value
  riot.store.trigger('update_dims','450')
}

下面的观察值不会更新界面,即使me.myCardHeight变量已经使用新的高度参数进行了更新:

代码语言:javascript
复制
me = this
riot.store.on('update_dims', function (height) {
  me.myCardHeight = height
  console.log(me.myCardHeight)
  me.update()
})

这样做的正确方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2017-03-16 04:49:31

只要你的商店是一个暴乱观察点,这应该是可行的:

代码语言:javascript
复制
riot.store = riot.observable();

var me = this
riot.store.on('update_dims', function (height) {
  me.myCardHeight = height;
  console.log(me.myCardHeight);
  me.update();
});

riot.store.trigger('update_dims', 450);

如果您对将其存储在对象中非常感兴趣,那么我更喜欢使用它自己的属性来创建一个共享存储:

代码语言:javascript
复制
var store = {
  observable: riot.observable(),
  height: 25 // default
}

riot.mount('myCard', {store: store})

然后在标记myCard

代码语言:javascript
复制
var me = this
me.myCardHeight = opts.store.height;

me.opts.store.on('update_dims', function (height) {
  me.opts.store.height = height;
  me.myCardHeight
  console.log(me.myCardHeight);
  me.update();
});

现在,当您调用它时,您将传入一个新的共享高度:

代码语言:javascript
复制
this.opts.trigger('update_dims', 450);

此外,请查看RiotControl,它可以与这种类型的体系结构一起使用。

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42208701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档