Module: Sequel::Plugins::ExtJS::DatasetMethods

Defined in:
lib/sequel_extjs.rb

Instance Method Summary collapse

Instance Method Details

#to_extjs(overwrite_count = nil) ⇒ Object

Generate everything ExtJS needs to display this dataset into a grid you can overwrite the returned count (in case you are using pagination) by supplying it as a first parameter this function also accepts a block that you can iterate over and modify the element (adding/removing keys) (more or less replicate map) this should work just about any dataset (including joins and stuff) For simple dataset to ExtJS use:

MyModel.to_extjs # all records
MyModel.filter(:type => 'primary').to_extjs # (only the ones with type 'primary')

Using blocks: This sample will return JSON structure that is the same as MyModel.to_extjs BUT with the added ‘newkey’ element for all records

MyModel.to_extjs do |mymodel|
  mymodel[:newkey] = 'somethingelse'
  mymodel
end

To remove a column(s) you have 2 options

1) use MyModel.select(:id,:name).to_extjs - it will display only the selected fields OR

2) use blocks to do it

MyModel.to_extjs do |swf|
  res = mymodel.values
  res[:newkey] = 'somethingelse'
  res.delete(:name)
  res
end


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sequel_extjs.rb', line 110

def to_extjs(overwrite_count = nil)
  data = all
  return '{}' if data.size == 0
  unless overwrite_count
    overwrite_count = data.size
  end

  if block_given?
    data.map! {|rec| yield rec}
  end
  fields = data.first.keys.map{|a| {:name => a}}

  {
    :totalCount => overwrite_count,
    :metaData => {
      :totalProperty => 'totalCount',
      :root => 'result',
      :id => first.primary_key,
      :fields => fields
    },
    :result => data
  }.to_json
end

#to_jsonObject

return a array of json hashes for the current dataset (i.e. current query result with multiple records)



79
80
81
# File 'lib/sequel_extjs.rb', line 79

def to_json
  map{|b| b.values}.to_json
end