0

我有一个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

到目前为止,表格工作正常。我想不出像我们在 rails 中那样实现 has_many 行为的方法fields_for

任何人都可以指导我如何使用netzke这种情况。

4

1 回答 1

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

请尝试让我知道这是否是您需要的。对不起,如果我误解了你的问题。

问候

德拉赞

于 2013-08-25T13:10:03.553 回答