2014-04-01 65 views
2

我有以下的車型,客戶設置一個屬於關聯屬性的區域的下拉列表中的客戶是 -通過行動控制器不工作

{{view Ember.Select viewName="select_area" content=possible_areas optionValuePath="content.id" optionLabelPath="content.name" value=area.id prompt="No area selected" selectionBinding="selected_area"}} 

和控制器,其導線了一切一起 -

App.CustomerController = Ember.ObjectController.extend({ 
    possible_areas: function() { 
     return this.store.find('area'); 
    }.property(), 

    selected_area: null, 
    actions: { 
     save: function() { 
      var selected_area_id = this.get('selected_area.id'); 
      console.log(selected_area_id); 
      var selected_area = this.store.find('area', selected_area_id); 
      var self = this; 
      selected_area.then(function(area) { 
       console.log(area.get('id')); 
       var updated_customer = self.get('model'); 
       updated_customer.set('area', area); 
       console.log(new_customer.get('area.id')); 
       updated_customer.save() 
        .then(function(customer) { 
         //success 
        }, 
        function() { 
         //failure 
        }); 
      }); 
     } 
    } 
}); 

現在這裏是奇怪的事情。當打電話的「拯救」行動的第一次,行updated_customer.set('area', area);失敗,錯誤

Uncaught Error: Assertion Failed: Cannot delegate set('id',) to the 'content' property of object proxy <DS.PromiseObject:ember551>: its 'content' is undefined. 

當打電話「保存」後,立即採取行動,保存經過沒有被成功更新客戶的任何錯誤和地區。儘管下拉菜單顯示selected_area爲空。

如何防止第一次保存錯誤?

我使用的是ember-data 1.0.0-beta.6。

回答

2

由於您在Customer模型中定義了關聯,我將從控制器中刪除selected_area屬性,並改爲使用ember-data的關聯。使用selectionBinding屬性綁定到Ember.Select中的「區域」關聯。

{{view Ember.Select viewName="select_area" 
        content=possible_areas 
        optionValuePath="content.id" 
        optionLabelPath="content.name" 
        prompt="No area selected" 
        selectionBinding="area"}} 

這樣,當用戶與選擇菜單交互的area屬性會發生變化。

由於我們直接綁定到Customerarea關聯,所以這還具有清理save操作的額外好處。

App.CustomerController = Ember.ObjectController.extend({ 
    possible_areas: function() { 
    return this.store.find('area'); 
    }, 

    actions: { 
    save: function() { 
     this.get('model').save().then(this.onDidCreate.bind(this), this.onDidFail.bind(this))  
    } 

    onDidCreate: function() { 
     // Fullfilled callback 
    } 

    onDidFail: function() { 
     // Rejected callback 
    } 
    } 
}); 

然而,possible_areas屬性不會被當模板,因爲this.get('area')回報承諾第一渲染填充。我會等待呈現選擇,直到承諾落定。您可以在路由model鉤子中執行此操作,因爲它會等待承諾解決以呈現模板。

App.CustomerRoute = Ember.Route.extend({ 
    route: function(params) { 
    return Ember.RSVP.hash({ 
     customer: this.store.find('customer', params.customer_id), 
     possible_areas: this.store.find('area') 
    }); 
    }, 

    // Since the route hook returns a hash of (hopefully) settled promises, we 
    // have to set the model property here, as well as the possible_areas property. 
    setupController: function(controller, model) { 
    controller.set('model', model.customer); 
    controller.set('possible_areas', model.possible_areas); 
    } 
}); 
+0

嗨菲克,謝謝你的答案。現在保存工作正常。但是,selectionBinding不起作用,因爲它不顯示選定的區域,無論是在第一次加載下拉菜單時還是在選擇新值之後。它總是顯示「No area selected」的提示。我嘗試添加'value = area.id',但仍然沒有正確顯示選定的區域。 保存方法仍然得到正確的區域,雖然奇怪。 – KaranK

+0

檢查我的編輯。我有一種感覺,這是一個時間問題。 – Feech

+0

我按照您的指定更改了路線。同樣的問題。 – KaranK