我正在开发一个项目管理网站。我正在使用Ember.js,Ember-data.js和Rails后端。到目前为止,我用一些测试数据填充了后端数据库。我可以对它们进行show
和index
操作非常好,并获得嵌套属性。接下来就是在其中一个上构建create
操作。我决定从Project
开始,其模型实现是:
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')
}
);
# == 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
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
class ProjectSerializer < ActiveModel::Serializer
attributes :id,
:title,
:starting_date,
:ending_date,
:description,
:creator_id
has_many :coworkers, embed: :id
end
我试图弄清楚Ember项目create
的行动是这样的:
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
方法,但我没有找到任何类型的指南或文档,我只是按照我发现的一些代码使用它们在这里或this great Ember Data Example from Dan Gebhardt。总而言之,我的问题是:
transactions
和createRecord
功能如何运作belongs_to
交易(create
属性)上插入creator_id
关系的ID属性has_many
交易(create
属性)coworkers
关系中插入一组id属性我真的很感激这方面的一些帮助。非常感谢。