Skip to content

Instantly share code, notes, and snippets.

@elia
Created September 8, 2014 12:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save elia/50a723e6133a645b4858 to your computer and use it in GitHub Desktop.
The View class
require 'opal-jquery'
class View
def self.selector= selector
@selector = selector
end
def self.selector
@selector
end
def self.create *args
Document.ready? { create!(*args) }
nil
end
def self.create! *args
instance = new(*args)
if instance.exist?
instances << instance
instance.setup
yield(instance) if block_given?
end
instance
end
def self.instances
@instances ||= []
end
def initialize(parent = nil, element = nil)
@element = element || (parent || Element).find(selector)
end
attr_reader :element
def exist?
element.any?
end
def selector
self.class.selector || raise(ArgumentError, 'missing selector')
end
def setup
# noop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment