2015-11-17 52 views
0

保存父到子我已經去了很多例子都在這裏SO和一些導遊/博客。似乎沒有任何工作。燼數據:通過選擇框

我有一個customer說的hasMany loads

目前的代碼是:

路線

export default Ember.Route.extend({ 
    setupController: function(controller, model) { 
    controller.setProperties(model); 
}, 
model: function() { 
return Ember.RSVP.hash({ 
    content: this.store.createRecord('truck-load'), 
    customerList: this.store.findAll('customer'), 
    equipmentList: this.store.findAll('equipment-list') 
    }); 
}, 
resetController(controller, isExisting) { 
    if (isExisting) { 
    var model = controller.get('model'); 
    if (model.get('isNew')) { 
     model.destroyRecord(); 
    } 
    } 
    } 
}); 

模板選擇框 - 兌現增加對燼-CLI

{{md-select content=customerList 
value=model.customer 
label="Customer" 
prompt="Please Choose a Customer..." 
optionLabelPath='content.name' 
optionValuePath='content.id'}} 

電流控制器 - 我已經試過這馬NY方式

export default Ember.Controller.extend({ 
    actions: { 
    save() { 
     var truckload = this.get('model'); 
     this.get('model.customer').then((customer) => { 
     truckload.set('customer', customer); 
     truckload.save().then((load) => { 
      this.get('notify').success('Truck Load Created'); 
      this.transitionToRoute('truck-loads.show', load.id); 
     }); 
     }); 

JSON我跑藥劑/鳳凰

Parameters: %{"data" => %{"attributes" => %{"pro_number" => "423432", "special" => nil, "status" => nil}, 
    "relationships" => %{"customer" => %{"data" => nil}, 
    "equipment_list" => %{"data" => nil}}} } 

客戶(和設備清單)JSON-API服務器都在零到來。

+0

你是不是調用'setupController'超強,所以'model'應該是不確定的。此外,您正在執行'model.customer',但從模型鉤子返回的哈希中沒有客戶密鑰。 – locks

+0

模式不是每灰燼督察不確定的。檢查員也顯示model.customer和吸氣/ setter函數以及客戶是屬於關聯承諾對象。 –

回答

0

這個固定。

1)將下拉結果設置爲控制器屬性 2)訪問此對象以查找模型並進行設置。

selectedCustomer: null, 
selectedEquipment: null, 
actions: { 
    save() { 
    var truckload = this.get('model'); 
    var customer_id = this.get('selectedCustomer'); 
    var equipment_id = this.get('selectedEquipment') 
    this.store.findRecord('customer', customer_id).then((customer) => { 
     truckload.set('customer', customer); 
     this.store.findRecord('equipmentList',equipment_id).then((equipment) => { 
      truckload.set('equipmentList', equipment); 
      truckload.save().then((load) => { 
      this.get('notify').success('Truck Load Created'); 
      this.transitionToRoute('truck-loads.show', load.id); 
      }); 
     }); 
     }); 

    return false; 
}, 

我懷疑這是做到這一點的最好方法 - 但它確實有效。