如何使用netzke实现每位用户的手风琴?

时间:2013-04-26 02:30:20

标签: ruby-on-rails extjs accordion netzke

以下是要求:

在边框布局UI中,用户(工作人员)网格位于西部,而中心的手风琴将在选择一名工作人员时在每个面板中显示所选用户的多个收藏集(例如奖励等)。

代码:

class StaffsAndAwards < Netzke::Base
  # Remember regions collapse state and size
  include Netzke::Basepack::ItemPersistence

  def configure(c)
    super
    c.items = [
      { netzke_component: :staffs, region: :west, width: 300, split: true },
      { netzke_component: :accordion, region: :center }
    ]
  end

  js_configure do |c|
    c.layout = :border
    c.border = false

    # Overriding initComponent
    c.init_component = <<-JS
      function(){
        // calling superclass's initComponent
        this.callParent();

        // setting the 'rowclick' event
        var view = this.getComponent('staffs').getView();
        view.on('itemclick', function(view, record){
          this.selectStaff({staff_id: record.get('id')});
          this.getComponent('awards').getStore().load();
        }, this);
      }
    JS
  end

  endpoint :select_staff do |params, this|
    component_session[:selected_staff_id] = params[:staff_id]
  end

  component :staffs do |c|
    c.klass = Netzke::Basepack::Grid
    c.model = "Staff"
    c.region = :west
  end

  component :awards do |c|
    c.kclass = Netzke::Basepack::Grid
    c.model = 'Award'
    c.data_store = {auto_load: false}
    c.scope = {:staff_id => component_session[:selected_staff_id]}
    c.strong_default_attrs = {:staff_id => component_session[:selected_staff_id]}
  end

  component :accordion do |c|
    c.klass = Netzke::Basepack::Accordion
    c.region = :center
    c.prevent_header = true
    c.items = [ { :title => "A Panel" }, :awards ]    # The error may occur here. :awards cannot be found. 
  end

end

错误是“NameError(未初始化的常量奖励)”。似乎:奖励组件无法找到,即使它已在上面定义。

一个组件可以嵌入另一个组件吗? 或者如何解决?感谢。

1 个答案:

答案 0 :(得分:2)

你在这里犯了一个相当常见的错误,声明:奖励组件是StaffsAndAwards的孩子,而它应该是手风琴的孩子。

很容易修复。单独声明你的手风琴组件(比方说,我们将其命名为AwardsAndStuff),将:award声明移到它上面,然后在StaffAndAwards中引用它:

component :accordion do |c|
  # if you call the component :awards_and_stuff instead of :accordion, next line is not needed
  c.klass = AwardsAndStuff

  # important - pass the staff_id
  c.staff_id = component_session[:selected_staff_id]

  c.region = :center
  c.prevent_header = true
end

在AwardsAndStuff内,您可以访问staff_id作为config.staff_id,并将其传递给:awards组件:

component :awards do |c|
  c.kclass = Netzke::Basepack::Grid
  c.model = 'Award'
  c.data_store = {auto_load: false}
  c.scope = {:staff_id => config.staff_id}
  c.strong_default_attrs = {:staff_id => config.staff_id}
end

通过这种方式,您还可以独立测试AwardsAndStuff。