Skip to content

Instantly share code, notes, and snippets.

@elia elia/view.js.rb
Created Sep 8, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.