7. Using of Ember.js
• UI handling
• Data transfer of Rails Model -> Ember Data
• Data transfer of Ember Data -> WebGL
• Ember.js Observer and WebGL
• Computation of Game character’s parameters
12. Between Rails and Ember Data
複雑または構造の変更が多いデータは、JSONテキストデータとしてRailsモデルのカラ
ムに保存。Ember DataのTransformを使い、JavaScriptでevalして、Emberオブジェクト
化している。
Definition of Ember Data Model A fixture of Rails Model (Factory Girl)
trait :as_2 do
id 2
ui_screen_id 1
screenIdentifier 'battle'
tableIdentifier 'character_0'
tableName '0:'
tableName_binding 'Tool.EScenarioCharacter.0.name'
selectable false
trs "[" +
"{"+
"'columns':[" +
"{" +
"'item':'HP:'," +
"'binding': 'Tool.EScenarioCharacter.0.hp'" +
"}" +
"]" +
"}," +
"{"+
"'columns':[" +
"{" +
"'item':'MP:'," +
"'binding': 'Tool.EScenarioCharacter.0.mp'" +
"}" +
"]" +
"}" +
"]"
end
var uiTableDefinition = {
screenIdentifier: DS.attr('string'),
tableIdentifier: DS.attr('string'),
tableName: DS.attr('string’),
tableName_binding: DS.attr('string'),
selectable: DS.attr('boolean'),
trs: DS.attr('uiTableTrs'),
…
};
Transform of Ember Data Model
window.Tool.UiTableTrsTransform =
DS.Transform.extend(
{
deserialize: function(value:any) {
var json:any = null;
eval("json = " + value);
return Ember.create(json);
}
});
13. Question : Ember Dataモデルの
一対多関係をRailsのモデルからどう復元する?
• RailsとEmber Dataの一体多のデータ表現では、一部異なる点がある。
• Railsでは、子モデルが親モデルの参照を持つだけで、親モデルから子モデルのリストにアク
セス可能。
• Ember Dataでは、親モデルが子モデルのid配列を明示的に持つ必要がある。
• 上記の違いがあるため、両者のやりとりには変換処理が必要。
App.Post = DS.Model.extend({
comments: DS.hasMany('comment', {async: true})
});
{
"post": {
"comments": [1, 2, 3]
}
}
App.Comment = DS.Model.extend({
post: DS.belongsTo('post')
});
{
"comment": {
"post": 1
}
}
class Post < ActiveRecord::Base
has_many :comments
end
create_table ”posts", force: true do |t|
end
class Comment < ActiveRecord::Base
belongs_to :post
end
create_table ”comments", force: true do |t|
t.integer "ui_screen_id”
end
14. Question : Ember Dataモデルの
一対多関係をRailsのモデルからどう復元する?
Ember Data Rails Model
var uiScreenDefinition = {
var uiTableDefinition = {
screenIdentifier: DS.attr('string'),
tableIdentifier: DS.attr('string'),
tableName: DS.attr('string’),
tableName_binding: DS.attr('string'),
selectable: DS.attr('boolean'),
trs: DS.attr('uiTableTrs'),
…
};
identifier: DS.attr('string'),
config: DS.attr('uiScreenConfig'),
uiTables: DS.hasMany('ui-table')
};
Definition of
‘uiScreen’
which has
many uiTables
Definition of
‘uiTable’
which belong
to ‘uiScreen’
create_table "ui_screens", force: true do |t|
t.string "identifier"
t.text "config"
t.text "uiTables”
end
class UiScreen < ActiveRecord::Base
has_many :ui_tables
end
create_table "ui_tables", force: true do |t|
t.integer "ui_screen_id"
t.string "screenIdentifier"
t.string "tableIdentifier"
t.string "tableName"
t.string "tableName_binding"
t.boolean "selectable"
t.text "trs”
end
class UiTable < ActiveRecord::Base
belongs_to :ui_screen
end
15. Answer : RailsのControllerでEmber Data
向けにhas many id配列を生成する
class Api::UiScreensController < ApplicationController
skip_before_action :verify_authenticity_token
def index
uiScreens = []
UiScreen.all.each do |uiScreen|
uiTablesStr = "["
uiScreen.ui_tables.each do |uiTable|
uiTablesStr += uiTable.id.to_s + ",“
end
uiTablesStr.chop!
uiTablesStr += "]"
uiScreen.update_attribute(:uiTables, uiTablesStr)
uiScreens.push(uiScreen)
end
jsonHash = JSON.parse(uiScreens.to_json) for item in jsonHash
item["uiTables"] = eval(item["uiTables"])
end
jsonWrapper = {}
jsonWrapper["ui_screens"] = jsonStr = jsonWrapper.to_json
jsonHash = JSON.parse(jsonStr)
render :json => jsonHash
end
uiScreenにbelong to して
いる全てのuiTableのidを
配列文字列に追加。
Ex:”[1, 2, 5, 6, 7]”
そして、JSON化して出力
{
"ui_screens": [
{
"config":
"{'firstUI':'command','hiddenUIs':['physics', 'magic',
'target', 'magic-fake'],'calculatePosition': true}",
"created_at": "2014-09-08T02:07:53.215Z",
"id": 1,
"identifier": "battle",
"uiTables": [1, 2, 5, 6, 7]
}
]
}