处理Store.save()以获取包含DS.BelongsTo

时间:2015-08-21 18:13:54

标签: ember.js ember-data

我有一个博客,使用JSON在Rails与Active Model Serializers v0.10.0.rc2和Ember.js之间进行通信。一切正常,直到我需要在发布模型上实现DS.BelongsTo才能从流派模型中获取标记名称。

大多数(特别是服务器端)似乎正在工作,因为{{model.genre.name})确实显示了在服务器端创建的帖子的类型名称(seeds.rb),但是当我发现时,事情总是变得很糟糕尝试在Ember方面创建一个帖子......我尝试了不同的方法,但是我无法通过帖子保存genre_id(虽然user_id已保存,但可能是因为我没有设置BelongsTo与它的关系。)

发布Create.js控制器

application: Ember.inject.controller('application'),
genreID: Ember.computed('application.selectedGID', function()
{
    return this.get('application.selectedGID');
}),

var store = this.store;
console.log(this.get('genreID')); //>> 1
var post = store.createRecord('post', {
    user_id: this.get('session.secure.userId'),
    genre_id: this.get('genreID'), //>> Was working before BelongsTo
    ...
    genre: store.find('genre', this.get('genreID')) //>> Supposed to work, but does not...
});
console.log(post.get('genre_id')); //>> 1
console.log(post.genre); //>> Computed Property
console.log(post.genre.id); //>> Undefined
console.log(post.get('genre.id'));  //>> Undefined
console.log(post.genre.name); //>> Undefined
console.log(post.get('genre.name'));  //>> Undefined
post.save().then(function() {
    self.transitionToRoute('post', post);
}, function() {
    alert('Failed to create Post...');
});

发布Index.js控制器

posts/1 << Created with seeds.rb

console.log(this.get('model.genre.id')); //>> 1
console.log(this.get('model.genre.name')); //>> Song

posts/18 << Created on Ember.js

console.log(this.get('model.genre.id')); //>> undefined
console.log(this.get('model.genre.name')); //>> undefined

终端

Processing by Api::V1::PostsController#create as JSON

Parameters: {"post"=>{"user_id"=>4, "fact_link"=>"bcvxvbxcbvxc", "fiction_link"=>"vbxcbcvxvbxcvbxc", "title"=>"vbxcxbvbxvccxvb", "importance"=>nil, "soft_delete"=>false, "soft_delete_date"=>nil, "hidden"=>false, "views_count"=>nil, "text"=>"bvxcbvcxbvcxvbxc", "comments_count"=>nil, "genre_id"=>nil, "fact_type_id"=>nil, "category_id"=>nil, "topic_id"=>nil}}

User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1  [["email", "user@example.com"]]
(0.1ms)  begin transaction

SQL (0.2ms)  INSERT INTO "posts" ("text", "views_count", "title", "user_id", "fact_link", "fiction_link", "soft_delete", "hidden", "comments_count", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["text", "bvxcbvcxbvcxvbxc"], ["views_count", nil], ["title", "vbxcxbvbxvccxvb"], ["user_id", 4], ["fact_link", "bcvxvbxcbvxc"], ["fiction_link", "vbxcbcvxvbxcvbxc"], ["soft_delete", "f"], ["hidden", "f"], ["comments_count", nil], ["created_at", "2015-08-21 17:27:01.399594"], ["updated_at", "2015-08-21 17:27:01.399594"]]

User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 4]]

SQL (0.1ms)  UPDATE "users" SET "posts_count" = COALESCE("posts_count", 0) + 1 WHERE "users"."id" = ?  [["id", 4]]

(17.5ms)  commit transaction

流派模式

posts: DS.hasMany('post', {async: true})
name: DS.attr ('string'),

发布模型

user_id: DS.attr ('number'), 
genre_id: DS.attr ('number'),
...
genre: DS.belongsTo('genre', {async: true})

可以在Github上找到整个源代码,但如果需要,我可以在此处添加更多代码段。

1 个答案:

答案 0 :(得分:0)

与Ember.js guide中所述相反,解决方案最终会以peekRecord代替findfindRecord使用,如下所述:< / p>

genre: store.peekRecord('genre', this.get('genreID'))

我认为这个问题的原因是find和findRecord创建的承诺,在调用store.save()之前没有得到解决。