Netzke有很多关系和rails fields_for

时间:2013-08-19 22:11:45

标签: extjs ruby-on-rails-3.2 netzke

我有一个简单的has_many关系,可以由rails fields_for实现。我正在为Netzke寻找相同的行为。

以下是课程:

class Question < ActiveRecord::Base
  attr_accessible :description, :title
  validates_presence_of :title, :description

  has_many :question_options
  accepts_nested_attributes_for :question_options

class QuestionOption < ActiveRecord::Base
  attr_accessible :title, :question
  validates_presence_of :title

  belongs_to :question

以下是我正在构建的表单:

class QuestionForm < Netzke::Basepack::Form

  js_configure do |c|
    c.mixin
  end

  def configure(c)
    super
    record.build_for_editing if record.present?
    c.model = 'Question'
    c.title = 'Question'
    c.items = [:title, :description]
  end    
end

到目前为止这种形式工作正常。我无法弄清楚实现has_many行为的方式,就像我们在rails fields_for

中所做的那样

任何人都可以指导我如何在此方案中使用netzke

1 个答案:

答案 0 :(得分:1)

您是否尝试实施主细节/一对多编辑?如果是这样,在Netzke演示应用程序中给出了一个很好的例子(参见http://netzke-demo.herokuapp.com/中的老板和文员)。

您需要创建三个组件:

  • 继承自Netzke :: Basepack :: Grid
  • 的问题组件(主)
  • 继承的QuestionOption组件(详细信息)
  • Netzke :: Basepack :: Grid Composite组件结合前两个
    组件并添加一些JavaScript来同步它们。

首先,QuestionOption模型必须允许访问主表的ID(Netzke的东西,不管它不起作用)。

class QuestionOption < ActiveRecord::Base
  attr_accessible :title, :question_id
  validates_presence_of :title
  belongs_to :question
end

这是组件代码:

class Questions < Netzke::Basepack::Grid
  def configure(c)
    super
    c.model = 'Question'
    c.items = [
      :description,
      :title
    ]
  end
end

class QuestionOptions < Netzke::Basepack::Grid
  def configure(c)
    super

    c.strong_default_attrs = {question_id: c.question_id}
    c.scope = {question_id: c.question_id}

    c.model = 'QuestionOption'
    c.columns = [
      { name: :title, header: 'Option Title'}
    ]
  end
end

class QuestionsAndOptions < Netzke::Base
  def configure(c)
    super
    c.items = [:questions, :question_options]
  end

  js_configure do |c|
    c.layout = :border
    c.border = false
    c.height = '100%'

    c.init_component = <<-JS
      function() {
        this.callParent();
        var questions = this.netzkeGetComponent('questions');
        var question_options = this.netzkeGetComponent('question_options');

        questions.on('itemclick', function(view, record) {
          this.selectQuestion(record.get('id'));
          question_options.getStore().load();
        }, this);
      }
    JS
  end

  endpoint :select_question do |id, this|
    component_session[:selected_question_id] = id
  end

  component :questions do |c|
    c.region = :center
  end

  component :question_options do |c|
    c.region = :south
    c.question_id = component_session[:selected_question_id]
    c.height = '50%'
  end
end

如果您需要,请尝试告诉我。对不起,如果我误解了你的问题。

此致

的Drazen

相关问题