使用Ember.js,Ember-data.js和Rails后端创建具有嵌套属性的操作

时间:2013-07-07 00:59:54

标签: ruby-on-rails transactions ember.js ember-data nested-attributes

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

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')
   }
);

PROJECT.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

我试图弄清楚Ember项目create的行动是这样的:

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方法,但我没有找到任何类型的指南或文档,我只是按照我发现的一些代码使用它们在这里或this great Ember Data Example from Dan Gebhardt。总而言之,我的问题是:

  1. transactionscreateRecord功能如何运作
  2. 如何在belongs_to交易(create属性)上插入creator_id关系的ID属性
  3. 如何在has_many交易(create属性)coworkers关系中插入一组id属性
  4. 我真的很感激这方面的一些帮助。非常感谢。

0 个答案:

没有答案