2015-08-21 66 views
-1

我有一個使用JSON與Active模式串行器v0.10.0.rc2和Ember.js軌間通信的博客記錄。一切工作,直到我需要實現對模型DS.BelongsTo從類型模型得到的標記名稱。處理Store.save()爲包含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", "[email protected]"]] 
(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找到,但我可以在必要時在這裏添加更多的代碼片段。

+0

確定是否on Rails的後端或sequilize expressjs使用Ruby? –

+0

Ruby on Rails。您可以檢查的Gemfile [這裏](https://github.com/Deovandski/Fakktion/blob/master/Gemfile) – Deovandski

+0

'帖子:DS.hasMany( '職位',{異步:真})'應該是'職位:DS.hasMany('post',{async:true})' –

回答

0

相反的是在Ember.js guide表示,該解決方案在某種程度上結束了使用peekRecord而不是findfindRecord如下所述:

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

我認爲,這個問題的原因是由find和findRecord創建的promise在store.save()被調用之前沒有得到解決。

+0

在旁註中,任何人都可以向我解釋downvote?這將有助於知道爲什麼... – Deovandski