私はプロジェクト管理 Web サイトに取り組んでいます。Ember.js、Ember-data.js、および Rails バックエンドを使用しています。ここまでで、バックエンド データベースにいくつかのテスト データを入力しました。show
それらに対する操作はindex
非常にうまく機能し、ネストされた属性も取得できます。create
次は、そのうちの 1 つでオペレーションを構築します。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
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
}
}
transactions
メソッドとメソッドの使用方法を検索してきましたがcreateRecord
、ガイドやドキュメントは見つかりませんでした。ここで見つけたいくつかのコードに従って、またはDan Gebhardt の素晴らしい Ember Data Example でそれらを使用しています。要約すると、私の質問は次のとおりです。
- 仕組み
transactions
とcreateRecord
機能 - トランザクションの
belongs_to
リレーションシップにid 属性を挿入する方法(属性)create
creator_id
- トランザクションの
has_many
関係から id 属性のセットを挿入する方法(属性)create
coworkers
これについて何か助けていただければ幸いです。よろしくお願いします。