Method: MustacheRender::RenderAble::ForList#to_mustache

Defined in:
lib/mustache_render/ables/render_able.rb

#to_mustache(options = {}, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mustache_render/ables/render_able.rb', line 102

def to_mustache options={}, &block

  result = {
    :any? => self.any?,
    :list => self.map do |item|
      item.to_mustache options, &block
    end
  }

  # 支持分页?
  # 分页相关的数据

  if self.respond_to?(:total_pages)
    total_entries = defined?(self.total_entries) ? self.total_entries : self.total_count

    pagination_result = {
      :support? => true,
      :info     => {
        :current_page  => self.current_page,                     # 当前页数
        :total_entries => total_entries,                         # 总页数
        :per_page      => self.per_page,                         # 每页条数
        :total_pages   => self.total_pages,                      # 总页数
        :first_page?   => self.current_page == 1,                # 是否是第一页?
        :last_page?    => self.current_page == self.total_pages, # 最后一页?
        :next_page     => self.next_page,                        # 下一页
        :previous_page => self.previous_page                     # 前一页
      }
    }
  else
    pagination_result = {
      :support? => false
    }
  end

  result.merge!(
    :pagination => pagination_result
  )

  result
end