extjs-mvc 0.1.16 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.16
1
+ 0.1.17
@@ -0,0 +1,48 @@
1
+ ##
2
+ # @class ExtJS::Component
3
+ #
4
+ class ExtJS::Component
5
+ attr_accessor :config
6
+ def initialize(controller, params)
7
+ @config = params.extract_options!
8
+ @controller = controller
9
+
10
+ @config[:items] = [] if config[:items].nil?
11
+
12
+ if container = @config.delete(:container)
13
+ container.add(self)
14
+ end
15
+ @partial_config = nil
16
+ end
17
+
18
+ def apply(params)
19
+ @config.merge!(params)
20
+ end
21
+
22
+ def add(*config)
23
+ options = config.extract_options!
24
+ if !options.keys.empty?
25
+ if url = options.delete(:partial)
26
+ # rendering a partial, cache the config until partial calls #add method. @see else.
27
+ @partial_config = options
28
+ return @controller.render(:partial => url, :locals => {:container => self})
29
+ else
30
+ options.merge!(@partial_config) unless @partial_config.nil?
31
+ @config[:items] << options
32
+ end
33
+ elsif !config.empty? && config.first.kind_of?(ExtJS::Component)
34
+ cmp = config.first
35
+ cmp.apply(@partial_config) unless @partial_config.nil?
36
+ @config[:items] << cmp.config
37
+ end
38
+ @partial_config = nil
39
+ end
40
+
41
+ def render
42
+ # If there are any listeners attached in json, we have to get rid of double-quotes in order to expose
43
+ # the javascript object.
44
+ # eg: "listeners":"SomeController.listeners.grid" -> {"listeners":SomeController.listeners.grid, ...}
45
+ json = @config.to_json.gsub(/\"listeners\":\s?\"([a-zA-Z\.\[\]\(\)]+)\"/, '"listeners":\1')
46
+ "Ext.ComponentMgr.create(#{json});"
47
+ end
48
+ end
@@ -0,0 +1,65 @@
1
+ ##
2
+ # ExtJS::Data::Store
3
+ #
4
+ module ExtJS::Data
5
+ class Store
6
+ def initialize(params)
7
+ options = params.extract_options!
8
+ options[:format] = 'json' if options[:format].nil?
9
+
10
+ @config = options[:config]
11
+
12
+ @format = options[:format]
13
+ @proxy = options[:proxy] || 'http'
14
+ @writer = options[:writer]
15
+ @type = (@proxy === 'direct' ? @proxy : @format).capitalize
16
+ @controller = "#{options[:controller].to_s.capitalize}Controller".constantize
17
+ @model = ((options[:model]) ? options[:model] : @controller.controller_name.singularize).camelize.constantize
18
+
19
+ # Merge Reader/Proxy config
20
+ @config.merge!(@controller.extjs_reader(@model))
21
+ @config.merge!(@controller.extjs_proxy(options))
22
+
23
+ # Set storeId implicitly based upon Model name if not set explicitly
24
+ @config["storeId"] = @model.to_s.downcase unless @config["storeId"]
25
+ end
26
+
27
+ ##
28
+ # pre-load a store with data. Not yet tested. In theory, this *should* work.
29
+ #
30
+ def load(*params)
31
+ #@config["loadData"] = @model.all(params).collect {|rec| rec.to_record }
32
+ end
33
+
34
+ ##
35
+ # renders the configured store
36
+ # @param {Boolean} script_tag [true] Not yet implemented. Always renders <script></script> tags.
37
+ def render(script_tag = true)
38
+ script = ''
39
+ # ugly hack for DirectProxy API. Have to add an Ext.onReady() after the Store constructor to set API
40
+ if @proxy === 'direct'
41
+ auto_load = @config.delete("autoLoad")
42
+ cname = @controller.capitalize
43
+ script = "Ext.onReady(function() { var s = Ext.StoreMgr.get('#{@config["storeId"]}');"
44
+ if (@options["directFn"])
45
+ script += "s.proxy.directFn = #{cname}.#{@config["directFn"]};"
46
+ else
47
+ script += "s.proxy.setApi({create:#{cname}.#{@config["api"]["create"]},read:#{cname}.#{@config["api"]["read"]},update:#{cname}.#{@config["api"]["update"]},destroy:#{cname}.#{@config["api"]["destroy"]}});"
48
+ end
49
+ if auto_load
50
+ script += "s.load();"
51
+ end
52
+ script += "});"
53
+ end
54
+
55
+ if @writer # <-- ugly hack because 3.0.1 can't deal with Writer as config-param
56
+ json = @config.to_json
57
+ json[json.length-1] = ','
58
+ json += "writer:new Ext.data.#{@format.capitalize}Writer(#{@writer.to_json})}"
59
+ "<script>new Ext.data.#{@type}Store(#{json});#{script}</script>"
60
+ else
61
+ "<script>new Ext.data.#{@type}Store(#{@config.to_json});#{script}</script>"
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/extjs-mvc.rb CHANGED
@@ -10,9 +10,14 @@ module ExtJS
10
10
  if defined?(ActiveRecord)
11
11
  require 'active_record/model'
12
12
  end
13
+ require 'extjs/component'
14
+ require 'extjs/data/store'
15
+
13
16
  require 'action_view/helpers/component'
14
- require 'action_controller/controller'
15
17
  require 'action_view/helpers/store'
18
+
19
+ require 'action_controller/controller'
20
+
16
21
  end
17
22
  end
18
23
 
@@ -0,0 +1,35 @@
1
+ ##
2
+ # ExtJS::Helpers::Component
3
+ #
4
+ module ExtJS::Helpers
5
+ module Component
6
+ ##
7
+ # add class-var @@extjs_on_ready
8
+ def self.included(controller)
9
+ controller.class_eval do
10
+ @@onready_queue = []
11
+ end
12
+ end
13
+
14
+ def extjs_component(*params)
15
+ ExtJS::Component.new(self, params)
16
+ end
17
+
18
+ ##
19
+ # Adds a script or ExtJS::Component instance to on_ready queue. The queue is emptied and rendered to
20
+ # <script></script> via #extjs_render
21
+ #
22
+ def extjs_onready(*params)
23
+ params.each do |cmp|
24
+ @@onready_queue << cmp
25
+ end
26
+ end
27
+
28
+ ##
29
+ # Empties the on_ready queue. Renders within <script></script> tags
30
+ #
31
+ def extjs_render
32
+ "<script>\nExt.onReady(function() {\n\t#{@@onready_queue.collect {|cmp| (cmp.kind_of?(ExtJS::Component)) ? cmp.render : cmp}.join("\n\t")}\n });\n</script>"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module ExtJS::Helpers
2
+ module Store
3
+ def extjs_store(*params)
4
+ ExtJS::Data::Store.new(params)
5
+ end
6
+ end
7
+ end
data/lib/mvc.rb CHANGED
@@ -10,10 +10,13 @@ module ExtJS
10
10
  if defined?(ActiveRecord)
11
11
  require 'active_record/model'
12
12
  end
13
- require 'action_controller/controller'
14
- require 'action_view/helpers/store'
15
- require 'action_view/helpers/component'
16
- end
17
- end
13
+ require 'extjs/component'
14
+ require 'extjs/data/store'
18
15
 
16
+ require 'helpers/component'
17
+ require 'helpers/store'
19
18
 
19
+ require 'action_controller/controller'
20
+
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extjs-mvc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Scott
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-04 00:00:00 -07:00
12
+ date: 2009-09-05 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -39,11 +39,13 @@ files:
39
39
  - Rakefile
40
40
  - VERSION
41
41
  - lib/action_controller/controller.rb
42
- - lib/action_view/helpers/component.rb
43
- - lib/action_view/helpers/store.rb
44
42
  - lib/active_record/model.rb
45
43
  - lib/dm/model.rb
46
44
  - lib/extjs-mvc.rb
45
+ - lib/extjs/component.rb
46
+ - lib/extjs/data/store.rb
47
+ - lib/helpers/component.rb
48
+ - lib/helpers/store.rb
47
49
  - lib/mvc.rb
48
50
  - test/mvc_test.rb
49
51
  - test/test_helper.rb
@@ -1,57 +0,0 @@
1
- module ExtJS::Helpers
2
- module Component
3
- def extjs_component(*params)
4
- ExtJS::Component.new(self, params.extract_options!)
5
- end
6
-
7
- def extjs_onready(*params)
8
- output = ''
9
- params.each do |cmp|
10
- output += (cmp.kind_of?(ExtJS::Component)) ? cmp.render : cmp
11
- end
12
- "<script>Ext.onReady(function() { #{output} });</script>"
13
- end
14
-
15
- end
16
- end
17
-
18
- class ExtJS::Component
19
- attr_accessor :config
20
- def initialize(controller, config)
21
- @controller = controller
22
- @config = config
23
- @config[:items] = [] if config[:items].nil?
24
-
25
- if container = @config.delete(:container)
26
- container.add(self)
27
- end
28
- @partial_config = nil
29
- end
30
-
31
- def apply(params)
32
- @config.merge!(params)
33
- end
34
-
35
- def add(*config)
36
- options = config.extract_options!
37
- if !options.keys.empty?
38
- if url = options.delete(:partial)
39
- # rendering a partial, cache the config until partial calls #add method. @see else.
40
- @partial_config = options
41
- return @controller.render(:partial => url, :locals => {:container => self})
42
- else
43
- options.merge!(@partial_config) unless @partial_config.nil?
44
- @config[:items] << options
45
- end
46
- elsif !config.empty? && config.first.kind_of?(ExtJS::Component)
47
- cmp = config.first
48
- cmp.apply(@partial_config) unless @partial_config.nil?
49
- @config[:items] << cmp.config
50
- end
51
- @partial_config = nil
52
- end
53
-
54
- def render
55
- "Ext.ComponentMgr.create(#{@config.to_json});"
56
- end
57
- end
@@ -1,48 +0,0 @@
1
- module ExtJS::Helpers
2
- module Store
3
- def extjs_store(params)
4
- params[:format] = 'json' if params[:format].nil?
5
- params[:proxy] = 'http' if params[:proxy].nil?
6
-
7
- controller = "#{params[:controller].to_s.capitalize}Controller".constantize
8
- model = ((params[:model]) ? params[:model] : params[:controller].singularize).capitalize.constantize
9
-
10
- reader = controller.extjs_reader(model)
11
- proxy = controller.extjs_proxy(params)
12
-
13
- params[:config]["storeId"] = model.to_s.downcase if params[:config]["storeId"].nil?
14
- params[:config].merge!(reader)
15
- params[:config].merge!(proxy)
16
-
17
- type = (params[:proxy] === 'direct' ? params[:proxy] : params[:format]).capitalize
18
-
19
- script = ''
20
- # ugly hack for DirectProxy API. Have to add an Ext.onReady() after the Store constructor to set API
21
- if params[:proxy] === 'direct'
22
- auto_load = params[:config].delete("autoLoad")
23
- cname = params[:controller].capitalize
24
- script = "Ext.onReady(function() { var s = Ext.StoreMgr.get('#{params[:config]["storeId"]}');"
25
- if (params[:config]["directFn"])
26
- script += "s.proxy.directFn = #{cname}.#{params[:config]["directFn"]};"
27
- else
28
- script += "s.proxy.setApi({create:#{cname}.#{params[:config]["api"]["create"]},read:#{cname}.#{params[:config]["api"]["read"]},update:#{cname}.#{params[:config]["api"]["update"]},destroy:#{cname}.#{params[:config]["api"]["destroy"]}});"
29
- end
30
- if auto_load
31
- script += "s.load();"
32
- end
33
- script += "});"
34
- end
35
-
36
- if params[:config]["writer"] # <-- ugly hack because 3.0.1 can't deal with Writer as config-param
37
- writer = params[:config].delete("writer")
38
- json = params[:config].to_json
39
- json[json.length-1] = ','
40
- json += "writer:new Ext.data.#{params[:format].capitalize}Writer(#{writer.to_json})}"
41
- "new Ext.data.#{type}Store(#{json});#{script}"
42
- else
43
- "new Ext.data.#{type}Store(#{params[:config].to_json});#{script}"
44
- end
45
-
46
- end
47
- end
48
- end