Module: ExtJS::Helpers::Store

Defined in:
lib/action_view/helpers/store.rb

Instance Method Summary collapse

Instance Method Details

#extjs_store(params) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/action_view/helpers/store.rb', line 3

def extjs_store(params)
  params[:format] = 'json' if params[:format].nil?
  params[:proxy] = 'http' if params[:proxy].nil?

  controller = "#{params[:controller].to_s.capitalize}Controller".constantize
  model = ((params[:model]) ? params[:model] : params[:controller].singularize).capitalize.constantize

  reader = controller.extjs_reader(model)
  proxy = controller.extjs_proxy(params)

  params[:config]["storeId"] = model.to_s.downcase
  params[:config].merge!(reader)
  params[:config].merge!(proxy)

  type = (params[:proxy] === 'direct' ? params[:proxy] : params[:format]).capitalize

  script = ''
  # ugly hack for DirectProxy API.  Have to add an Ext.onReady() after the Store constructor to set API
  if params[:proxy] === 'direct'
    auto_load = params[:config].delete("autoLoad")
    cname = params[:controller].capitalize
    script = "Ext.onReady(function() { var s = Ext.StoreMgr.get('#{model.to_s.downcase}');"
    if (params[:config]["directFn"])
      script += "s.proxy.directFn = #{cname}.#{params[:config]["directFn"]};"
    else
      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"]}});"
    end
    if auto_load
      script += "s.load();"
    end
    script += "});"
  end

  if params[:config]["writer"]  # <-- ugly hack because 3.0.1 can't deal with Writer as config-param
    writer = params[:config].delete("writer")
    json = params[:config].to_json
    json[json.length-1] = ','
    json += "writer:new Ext.data.#{params[:format].capitalize}Writer(#{writer.to_json})}"
    "new Ext.data.#{type}Store(#{json});#{script}"
  else
    "new Ext.data.#{type}Store(#{params[:config].to_json});#{script}"
  end

end