温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - calling a function on a bound property change in aurelia
aurelia javascript typescript aurelia-store

javascript - 在aurelia的绑定属性更改上调用函数

发布于 2020-05-12 18:26:15

我有这个viewmodel,并且所有代码都有效

@connectTo<State>({
  selector: (store) => store.state.pipe(pluck('domain')).pipe(pluck('games')),
  target: 'games',
})
@connectTo<State>({
  selector: (store) => store.state.pipe(pluck('domain')).pipe(pluck('myGames')),
  target: 'myGames',
})
@autoinject()
@customElement('games')
export default class Games {
  private static readonly ADD_TO_MYGAMES = 'addToMyGames';
  @bindable() games: Game[] = [];
  myGames: Game[] = [];

  constructor(
    private readonly store: Store<State>,
  ) {
    store.registerAction(Games.ADD_TO_MYGAMES, myGamesState);
  }

  available(game: Game): boolean {
    console.log("available", game);
    return !!this.myGames.find((i) => _.isEqual(i, game));
  }

  addGame(game: Game) {
    this.store.dispatch(Games.ADD_TO_MYGAMES, game);
  }
}

const myGamesState = async (current: State, game: Game) => {
  console.log(game);
  return produce(current, state => {
    state.domain.myGames.push(game);
  });
}

问题是当myGames作为新游戏时,视图不会刷新 available

<template bindable="games">
  <div class="columns">
    <ul class="column">
      <li repeat.for="game of games" class="level">
        <button class="button level-item is-fullwidth ${available(game) ? 'is-success' : ''}" click.delegate="addGame(game)"
          disabled.bind="available(game)">
          ${game.name}
        </button>
      </li>
    </ul>
  </div>
</template>

我应该如何解决这个问题?

查看更多

提问者
xenoterracide
被浏览
10
weagle08 2020-02-24 11:30

我认为您正在执行功能参考。您将要使用:
disabled.call="available(game)"

请参阅:Aurelia中的函数引用