我如何使用ember数据集属于关系?

时间:2014-02-24 08:14:17

标签: javascript ruby-on-rails ember.js ember-data

我看这个指南使用ember数据集之间的关系,两个模型之间。 http://emberjs.com/guides/models/creating-and-deleting-records/

但它不起作用!我的代码是:

QuotationsNewController:

App.QuotationsNewController = Em.ObjectController.extend
  content: []

  actions:
    save: ->
      quotation =
        actualNumber: @get('actualNumber')
      quotation = @store.createRecord('quotation', quotation)
      @store.find('user', @get('userId')).then (user)->
        quotation.set('user', user)
      quotation.save().then(@onDidCreate.bind(@))
      @set('quotation', '')

  onDidCreate: (quotation)->
    @store.push 'quotation', quotation.get('data')
    @transitionToRoute 'quotation', quotation

QuotationsNewRoute:

App.QuotationsNewRoute = Ember.Route.extend

  setupController: (controller)->
    controller.set('users', @store.find('user'))
    controller.set('userId', @auth.get('userId'))

新模板:

<form {{action 'save' on="submit"}} class="form-horizontal label-left">
  ...
  <div class="controls form-group">
    {{select-input valueBinding="userId"
                   contentBinding="users"
                   optionValuePath="content.id"
                   optionLabelPath="content.name"
                   class="select-block-level"}}
  </div>
</form>

报价模型:

App.Quotation = DS.Model.extend
  ...
  user: DS.belongsTo 'user'

用户模型:

App.User = DS.Model.extend
  quotations: DS.hasMany('quotation', async: true)

版本:

DEBUG: ------------------------------- 
DEBUG: Ember      : 1.4.0 
DEBUG: Ember Data : 1.0.0-beta.7+canary.f482da04 
DEBUG: Handlebars : 1.3.0 
DEBUG: jQuery     : 1.11.0 
DEBUG: ------------------------------- 

我认为它可以设置用户和引用关系,但我看到用户总是为空"user_id"=>nil。我不知道为什么user_id总是为零。

0 个答案:

没有答案