大家好,谢谢你们查看一下。我是Ember的新手,我的注册问题和hasMany关系没有保存。
我有一个Scout模型,我已经创建了一个尚未分配给userAccount的工具。
App.Scout = DS.Model.extend(
product: DS.attr('string')
condition: DS.attr('string')
price: DS.attr('number')
userAccount: DS.belongsTo('userAccount')
)
App.Scout.FIXTURES = [
{
id: 1
product: 'Clothing Hamper'
condition: 'Not Smelly'
price: 25
}
]
App.UserAccount = DS.Model.extend
email: DS.attr('string')
password: DS.attr('string')
scouts: DS.hasMany('scout')
然后我转到创建的侦察路径并注册用户并尝试创建用户和侦察之间的关系。用户注册后,它会转到交换机,检查App.currentUser.get('scouts.length')并将其重新路由到侦察路径,如果= = 1。
App.Router.map ->
this.resource 'switchboard'
this.resource 'scout', { path: '/scouts/:scout_id' }
App.ScoutRoute = Ember.Route.extend
model: (params) ->
@store.find 'scout', params.scout_id
afterModel: ->
console.log("after transition currentUser's scout length: {App.currentUser.get('scouts.length')}") unless App.currentUser is null
actions:
createAccount: ->
@currentModel.set('userAccount', App.currentUser)
App.currentUser.get('scouts').pushObject(@currentModel)
App.currentUser.save()
App.SwitchboardRoute = Ember.Route.extend
beforeModel: ->
console.log("before transition currentUser's scout length: {App.currentUser.get('scouts.length')}")
if App.currentUser.get('scouts.length') == 1
@transitionTo('scout', App.currentUser.get('scouts.firstObject').id)
App.ScoutController = Ember.ObjectController.extend
foo: App.get('currentUser.scouts.length')
App.UsersNewController = Ember.ObjectController.extend(
identification: null
password: null
actions:
createAccount: ->
user = @store.createRecord('userAccount',
email: @get('identification')
password: @get('password')
)
user.save()
@transitionToRoute('switchboard')
App.currentUser = user
true
)
这对于重新路由确实相同,但在重新路由完成后,当我在控制台App.currentUser.get('scouts.length)中键入它或查看ember检查器数据时,用户没有侦察附在它上面。此外,userAccount仍然附加到Scout上,就像它没有相反地保存它一样。
非常感谢任何帮助。我还创建了一个jsbin来查看它,http://jsbin.com/ENAWotu/17/edit
谢谢!