2

我正在开发一个项目管理网站。我正在使用 Ember.js、Ember-data.js 和 Rails 后端。到目前为止,我用一些测试数据填充了后端数据库。我可以对它们进行很好的操作show和操作,并且也可以获得嵌套属性。index接下来是create在其中一个上构建操作。我决定从 开始Project,其模型实现是:

项目.JS

App.Adapter.map
(
  'App.Project',
  {
    creator: {embedded: 'always'},
    coworkers: {embedded: 'always'}
  }
);


App.Project = DS.Model.extend
(
   {
      title: DS.attr('string'),
      creator: DS.belongsTo('App.Person', {embedded: 'always'}),
      coworkers: DS.hasMany('App.Person', {embedded: 'always'}),
      startingDate: DS.attr('string'),
      endingDate: DS.attr('string'),
      description: DS.attr('string')
   }
);

项目.RB

# == Schema Information
#
# Table name: projects
#
#  id            :integer          not null, primary key
#  title         :string(255)
#  starting_date :date
#  ending_date   :date
#  description   :string(255)
#  creator_id    :integer
#  created_at    :datetime         not null
#  updated_at    :datetime         not null

class Project < ActiveRecord::Base

  attr_accessible :title, :startingDate, :endingDate, :description, :creator_id
  validates :title, presence: true

  ####################################################################
  ## ONE-TO-MANY RELATION FOR CREATOR                               ##
  ####################################################################
  belongs_to :creator,
    foreign_key: "creator_id",
    class_name: "Person"

  ####################################################################
  ## MANY-TO-MANY RELATION FOR COWORKERS - JOIN TABLE: PROJECT_TEAM ##
  ####################################################################
  has_many :project_teams,
    foreign_key: "project_id",
    class_name: "ProjectTeam"
  has_many :coworkers,
    through: :project_teams
end

PROJECT_CONTROLLER.RB

class ProjectsController < ApplicationController

  # GET /project.json
  def index
    render json: Project.all
  end

  # GET /projects/1.json
  def show
    p = Project.find(params[:id])
    render json: p
  end

  # POST /projects.json
  def create
    p = Project.new(params[:project])
    p.save!
    p.reload
  end
end

PROJECT_SERIALIZER.RB

class ProjectSerializer < ActiveModel::Serializer
  attributes :id,
             :title,
             :starting_date,
             :ending_date,
             :description,
             :creator_id

  has_many :coworkers, embed: :id
end

我试图create对 Ember 项目采取的行动是:

PROJECT_NEW_CONTROLLER.JS

App.ProjectsNewController = Ember.ObjectController.extend
(
  {
    needs: ['app'],
    projectTitle: null,
    startingDate: null,
    endingDate: null,
    description: null,

    createProject: function()
    {
      var pTitle =        this.get('projectTitle');
      var pStartingDate = this.get('startingDate');
      var pEndingDate =   this.get('endingDate');
      var pDescription =  this.get('description');

      var personId = this.get('controllers.app.personId');

      this.transaction = this.get('store').transaction();

      var newProject = this.transaction.createRecord
      (
        App.Project,
        {
          title: pTitle,
          startingDate: pStartingDate,
          endingDate: pEndingDate,
          description: pDescription,
          creator_id: parseInt(personId, 10)
        }
      );

      this.set('model', newProject);

      this.transaction.commit();
      this.transaction = null;
    }
  }
);

到目前为止,标准属性运行良好。但是我无法使用该creator_id属性,甚至无法使用coworkers数组属性。使用 Firebug 发现的最大近似值是检查我的属性是否正确分配到this.transaction

this.transaction.records.list[0].get('title')           "Project title"
this.transaction.records.list[0].get('startingDate')    "17/07/2013"
this.transaction.records.list[0].get('creator_id')      1
...

但是我的 JSON 帖子是这样的:

{
    "project": 
    {
        "title": "Project title",
        "starting_date": "17/07/2013",
        "ending_date": "30/08/2013",
        "description": "A brief description",
        "creator_id": null
    }
}

我一直在寻找如何使用transactionscreateRecord方法,但我没有找到任何类型的指南或文档,我只是按照我在这里找到的一些代码或Dan Gebhardt 的这个伟大的 Ember 数据示例来使用它们。所以,总结一下,我的问题是:

  1. 工作原理transactionscreateRecord功能
  2. 如何为事务(属性)belongs_to上的关系插入 id 属性createcreator_id
  3. 如何从事务(属性)的has_many关系中插入一组 id 属性createcoworkers

我真的很感激这方面的一些帮助。提前非常感谢。

4

0 回答 0