为我的Rails应用程序实现观察者模式 [英] Implementing Observer Pattern for my Rails App

查看:84
本文介绍了为我的Rails应用程序实现观察者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Rails 3.0.7和ruby 1.9.2,并制作一个Rails App,其中包含在从FlowPlayer渲染时从数据库加载的视频.以及基于视频的一组幻灯片.现在,我想将幻灯片与视频同步.对于计时,我要求用户输入每张幻灯片的计时.因此,我想知道是否可以通过将某种中心时间作为主体,将视频和幻灯片作为观察者来使用观察者模式?

I am currently using rails 3.0.7 and ruby 1.9.2 and making a rails App which contains a video being loaded from a database while being rendered by FlowPlayer. and a set of slides based on the video. Now, I wanted to synchronize the slides with the video. For the timings, I am asking the user to enter the timings of each slide. So, I was wondering if i could use observer pattern for this by making a sort of central time as the subject and the video and slides as the observers?

虽然在网上浏览了许多教程后,这个概念似乎是正确的,但我无法继续进行或提出有关编码的想法.

While, the concept seems correct after going through a number of tutorials on the net, I am not able to proceed or ge ideas on coding it.

任何帮助将不胜感激.

推荐答案

基本上,您有两种选择:

basically, you have two choices:

使用ActiveRecord的回调(before_save,after_save等)或创建观察者

use ActiveRecord's callbacks (before_save, after_save, etc..) or create an observer

# app/observers/some_model_observer.rb
class SomeModelObserver < ActiveRecord::Observer
  observe :your_model # the model you're observing

  def after_create(record)
  end

  # other ActiveRecord callbacks
end

这篇关于为我的Rails应用程序实现观察者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆