首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在aasm事件上返回值?

如何在aasm事件上返回值?
EN

Stack Overflow用户
提问于 2010-12-10 15:09:34
回答 1查看 1.2K关注 0票数 3

如何使aasm事件返回布尔值以外的值?我使用的是aasm 2.2.0

例如,有一个MusicPlayer模型,它在启动时随机播放一首歌曲

代码语言:javascript
复制
aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly and return the song object
end

现在,如果我想在启动播放器时返回当前正在播放的歌曲,我该如何通过'play_song‘方法来实现呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-20 16:36:32

你不能这么做。返回状态用于指示转换是否成功。但是我很好奇你有什么用例需要它。

但是您可以包装状态转换并使用由play_song方法设置的变量,如下所示:

代码语言:javascript
复制
aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly
  @song = ...
end

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

https://stackoverflow.com/questions/4406504

复制
相关文章

相似问题

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