mirror of
https://github.com/zhigang1992/rmq.git
synced 2026-04-30 21:52:33 +08:00
Added convenience callback block after append, build, etc
This commit is contained in:
@@ -102,7 +102,7 @@ class MainController < UIViewController
|
|||||||
|
|
||||||
def init_popup_section
|
def init_popup_section
|
||||||
|
|
||||||
rmq.append(UIView, :popup_section).tap do |q|
|
rmq.append(UIView, :popup_section) do |q|
|
||||||
|
|
||||||
@title_label = q.append!(UILabel, :title_label)
|
@title_label = q.append!(UILabel, :title_label)
|
||||||
q.append(UIButton, :open_popup).on(:touch_down) do |sender|
|
q.append(UIButton, :open_popup).on(:touch_down) do |sender|
|
||||||
@@ -116,7 +116,7 @@ class MainController < UIViewController
|
|||||||
|
|
||||||
end.on(:touch_up) do |sender|
|
end.on(:touch_up) do |sender|
|
||||||
|
|
||||||
rmq.append(UILabel, :popup_wrapper).tap do |o|
|
rmq.append(UILabel, :popup_wrapper) do |o|
|
||||||
o.animations.fade_in
|
o.animations.fade_in
|
||||||
o.append(UILabel, :popup_text_label)
|
o.append(UILabel, :popup_text_label)
|
||||||
end
|
end
|
||||||
@@ -132,7 +132,7 @@ class MainController < UIViewController
|
|||||||
def init_validation_section
|
def init_validation_section
|
||||||
|
|
||||||
# let's lay this out using the grid!
|
# let's lay this out using the grid!
|
||||||
rmq.append(UIView, :validation_section).tap do |q|
|
rmq.append(UIView, :validation_section) do |q|
|
||||||
q.append(UILabel, :validation_title)
|
q.append(UILabel, :validation_title)
|
||||||
@digits_only = q.append(UITextField, :only_digits).validates(:digits).on(:change) do |sender|
|
@digits_only = q.append(UITextField, :only_digits).validates(:digits).on(:change) do |sender|
|
||||||
rmq(sender).valid?
|
rmq(sender).valid?
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ module RubyMotionQuery
|
|||||||
# @return [RMQ]
|
# @return [RMQ]
|
||||||
def add_subview(view_or_constant, opts={})
|
def add_subview(view_or_constant, opts={})
|
||||||
subviews_added = []
|
subviews_added = []
|
||||||
style = opts[:style]
|
|
||||||
|
|
||||||
selected.each do |selected_view|
|
selected.each do |selected_view|
|
||||||
created = false
|
created = false
|
||||||
@@ -63,11 +62,14 @@ module RubyMotionQuery
|
|||||||
new_view.rmq_appended if appended
|
new_view.rmq_appended if appended
|
||||||
|
|
||||||
if self.stylesheet
|
if self.stylesheet
|
||||||
apply_style_to_view(new_view, style) if style
|
apply_style_to_view(new_view, opts[:style]) if opts[:style]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
RMQ.create_with_array_and_selectors(subviews_added, selectors, @context, self)
|
view = RMQ.create_with_array_and_selectors(subviews_added, selectors, @context, self)
|
||||||
|
opts[:block].call view if opts[:block]
|
||||||
|
opts[:raw_block].call view.get if opts[:raw_block]
|
||||||
|
view
|
||||||
end
|
end
|
||||||
alias :insert :add_subview
|
alias :insert :add_subview
|
||||||
|
|
||||||
@@ -93,8 +95,9 @@ module RubyMotionQuery
|
|||||||
# rmq.append(UIImageView)
|
# rmq.append(UIImageView)
|
||||||
#
|
#
|
||||||
# @return [RMQ]
|
# @return [RMQ]
|
||||||
def append(view_or_constant, style=nil, opts = {})
|
def append(view_or_constant, style=nil, opts = {}, &block)
|
||||||
opts[:style] = style
|
opts[:style] = style
|
||||||
|
opts[:block] = block if block
|
||||||
add_subview(view_or_constant, opts)
|
add_subview(view_or_constant, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -103,16 +106,18 @@ module RubyMotionQuery
|
|||||||
# @example
|
# @example
|
||||||
# @my_button = rmq.append! UIButton
|
# @my_button = rmq.append! UIButton
|
||||||
# @my_label = rmq.append!(UILabel, :my_label)
|
# @my_label = rmq.append!(UILabel, :my_label)
|
||||||
def append!(view_or_constant, style=nil, opts = {})
|
def append!(view_or_constant, style=nil, opts = {}, &block)
|
||||||
|
opts[:raw_block] = block if block
|
||||||
append(view_or_constant, style, opts).get
|
append(view_or_constant, style, opts).get
|
||||||
end
|
end
|
||||||
|
|
||||||
# Just like append, but inserts the view at index 0 of the subview array
|
# Just like append, but inserts the view at index 0 of the subview array
|
||||||
#
|
#
|
||||||
# @return [RMQ]
|
# @return [RMQ]
|
||||||
def unshift(view_or_constant, style=nil, opts = {})
|
def unshift(view_or_constant, style=nil, opts = {}, &block)
|
||||||
opts[:at_index] = 0
|
opts[:at_index] = 0
|
||||||
opts[:style] = style
|
opts[:style] = style
|
||||||
|
opts[:block] = block if block
|
||||||
add_subview view_or_constant, opts
|
add_subview view_or_constant, opts
|
||||||
end
|
end
|
||||||
alias :prepend :unshift
|
alias :prepend :unshift
|
||||||
@@ -122,7 +127,8 @@ module RubyMotionQuery
|
|||||||
# @example
|
# @example
|
||||||
# @my_button = rmq.prepend! UIButton
|
# @my_button = rmq.prepend! UIButton
|
||||||
# @my_label = rmq.prepend!(UILabel, :my_label)
|
# @my_label = rmq.prepend!(UILabel, :my_label)
|
||||||
def unshift!(view_or_constant, style=nil, opts = {})
|
def unshift!(view_or_constant, style=nil, opts = {}, &block)
|
||||||
|
opts[:raw_block] = block if block
|
||||||
unshift(view_or_constant, style, opts).get
|
unshift(view_or_constant, style, opts).get
|
||||||
end
|
end
|
||||||
alias :prepend! :unshift!
|
alias :prepend! :unshift!
|
||||||
@@ -147,10 +153,11 @@ module RubyMotionQuery
|
|||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
def create(view_or_constant, style = nil, opts = {})
|
def create(view_or_constant, style = nil, opts = {}, &block)
|
||||||
# TODO, refactor so that add_subview uses create, not backwards like it is now
|
# TODO, refactor so that add_subview uses create, not backwards like it is now
|
||||||
opts[:do_not_add] = true
|
opts[:do_not_add] = true
|
||||||
opts[:style] = style
|
opts[:style] = style
|
||||||
|
opts[:block] = block if block
|
||||||
add_subview view_or_constant, opts
|
add_subview view_or_constant, opts
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -158,7 +165,8 @@ module RubyMotionQuery
|
|||||||
#
|
#
|
||||||
# @example
|
# @example
|
||||||
# @my_button = rmq.create! UIButton
|
# @my_button = rmq.create! UIButton
|
||||||
def create!(view_or_constant, style=nil, opts = {})
|
def create!(view_or_constant, style=nil, opts = {}, &block)
|
||||||
|
opts[:raw_block] = block if block
|
||||||
create(view_or_constant, style, opts).get
|
create(view_or_constant, style, opts).get
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -174,9 +182,10 @@ module RubyMotionQuery
|
|||||||
# def rmq_build
|
# def rmq_build
|
||||||
# rmq.append(UIView, :foo)
|
# rmq.append(UIView, :foo)
|
||||||
# end
|
# end
|
||||||
def build(view, style = nil, opts = {})
|
def build(view, style = nil, opts = {}, &block)
|
||||||
opts[:do_not_add] = true
|
opts[:do_not_add] = true
|
||||||
opts[:style] = style
|
opts[:style] = style
|
||||||
|
opts[:block] = block if block
|
||||||
add_subview view, opts
|
add_subview view, opts
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -184,7 +193,8 @@ module RubyMotionQuery
|
|||||||
#
|
#
|
||||||
# @example
|
# @example
|
||||||
# @my_cell = rmq.build! cell
|
# @my_cell = rmq.build! cell
|
||||||
def build!(view, style = nil, opts = {})
|
def build!(view, style = nil, opts = {}, &block)
|
||||||
|
opts[:raw_block] = block if block
|
||||||
build(view, style, opts).get
|
build(view, style, opts).get
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ describe 'subviews' do
|
|||||||
@vc.view.subviews.first.should == view
|
@vc.view.subviews.first.should == view
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should addend to the end of the subviews' do
|
it 'should append to the end of the subviews' do
|
||||||
view = @vc.rmq.append(UIView).get
|
view = @vc.rmq.append(UIView).get
|
||||||
@vc.view.subviews[@vc.view.subviews.length - 1].should == view
|
@vc.view.subviews[@vc.view.subviews.length - 1].should == view
|
||||||
end
|
end
|
||||||
@@ -138,6 +138,25 @@ describe 'subviews' do
|
|||||||
#TODO
|
#TODO
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should insert then yield to a block with the rmq object' do
|
||||||
|
block_called = false
|
||||||
|
@vc.rmq.append(UILabel) do |view|
|
||||||
|
view.should.be.kind_of(RubyMotionQuery::RMQ)
|
||||||
|
view.get.should.be.kind_of(UILabel)
|
||||||
|
block_called = true
|
||||||
|
end
|
||||||
|
block_called.should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should insert then yield to a block with the created view' do
|
||||||
|
block_called = false
|
||||||
|
@vc.rmq.append!(UILabel) do |view|
|
||||||
|
view.should.be.kind_of(UILabel)
|
||||||
|
block_called = true
|
||||||
|
end
|
||||||
|
block_called.should == true
|
||||||
|
end
|
||||||
|
|
||||||
describe 'create' do
|
describe 'create' do
|
||||||
it 'should allow you to create a view using rmq, without appending it to the view tree' do
|
it 'should allow you to create a view using rmq, without appending it to the view tree' do
|
||||||
test_view_wrapped = @vc.rmq.create(SubviewTestView)
|
test_view_wrapped = @vc.rmq.create(SubviewTestView)
|
||||||
@@ -203,6 +222,26 @@ describe 'subviews' do
|
|||||||
table_sub.class.should == SubTableTest
|
table_sub.class.should == SubTableTest
|
||||||
table_sub.style.should == UITableViewStyleGrouped
|
table_sub.style.should == UITableViewStyleGrouped
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should create then yield to a block with the rmq object' do
|
||||||
|
block_called = false
|
||||||
|
@vc.rmq.create(UILabel) do |view|
|
||||||
|
view.should.be.kind_of(RubyMotionQuery::RMQ)
|
||||||
|
view.get.should.be.kind_of(UILabel)
|
||||||
|
block_called = true
|
||||||
|
end
|
||||||
|
block_called.should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should create then yield to a block with the created view' do
|
||||||
|
block_called = false
|
||||||
|
@vc.rmq.create!(UILabel) do |view|
|
||||||
|
view.should.be.kind_of(UILabel)
|
||||||
|
block_called = true
|
||||||
|
end
|
||||||
|
block_called.should == true
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'build' do
|
describe 'build' do
|
||||||
@@ -273,6 +312,27 @@ describe 'subviews' do
|
|||||||
@vc.rmq.append(view)
|
@vc.rmq.append(view)
|
||||||
view.number_of_builds.should == 1
|
view.number_of_builds.should == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should build an existing view then yield to a block with the rmq wrapped view' do
|
||||||
|
existing_view = UIView.new
|
||||||
|
block_called = false
|
||||||
|
@vc.rmq.build(existing_view) do |view|
|
||||||
|
view.should.be.kind_of(RubyMotionQuery::RMQ)
|
||||||
|
view.get.should == existing_view
|
||||||
|
block_called = true
|
||||||
|
end
|
||||||
|
block_called.should == true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should build an existing view then yield to a block with the existing view' do
|
||||||
|
existing_view = UIView.new
|
||||||
|
block_called = false
|
||||||
|
@vc.rmq.build!(existing_view) do |view|
|
||||||
|
view.should == existing_view
|
||||||
|
block_called = true
|
||||||
|
end
|
||||||
|
block_called.should == true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user