2013-08-24 60 views
0

我正在從App.Resource.find()獲取記錄列表這工作正常。如何更新餘燼模型中的記錄

對於每一行數據,都有一個固定值的下拉框。當對下拉框進行更改時,我希望向服務器發出POSTPUT請求,以便用下拉菜單中新選擇的值更新該行。

我有兩件事情煩惱:

  • 我怎樣才能獲得ID和在我的模型或控制器
  • 選擇了下拉的value我怎樣才能把這些價值觀和發出的請求服務器。有沒有App.Resource.update...

我與本地數據的工作示例的jsBin:http://jsbin.com/OcAyoYo/84/edit

回答

0

好吧,這裏有一個方法來獲得所選擇的價值,讓我們把東西放在哪裏,他們應該去,OK,首先,讓我們創建一個控制器,它會來裝飾你的反應記錄,你混的名字,你使用後,所以,我會用這個名字,這裏是控制器:

App.PostController = Ember.ObjectController.extend({ 
    selectedChanged: function() { 
    //here, you have access to the selected value with this: 
    // this.get('selected') 

    //And also, this controller represents you object rendered on the screen, 
    //so, you can change it here if you want like this: 
    //this.set('whataverPropertyYouWantToChange', 'newValue'); 

    //then you can save this record(send the request to the server) by just doing this: 
    //this.save(); this will make a request to the server 
    }.observes('selected') 
}); 

然後,才能使用該控制器,更改您將渲染記錄的循環,更改爲:

{{#each model itemController="post"}} 
    <tr> 
    <td>{{author}}</td> 
    <td>{{book}}</td> 
    <td> 
     {{view Ember.Select 
     contentBinding= 'App.names.content' 
     selectionBinding='selected'}} 
    </td> 
    </tr> 
{{/each}} 

只是要小心,在PostController中,即使'selected'屬性具有空值,也會觸發觀察者,您需要驗證它是否爲空。

+0

我試過這種方法,但它不工作。例如,我從控制器提醒'this.get(「selected」)',但它什麼都不做。事實上,我認爲在我選擇下拉框時它甚至不會被觸發。 ://jsbin.com/OcAyoYo/107/edit – Anthony

+0

你一定是做錯了,我試着在你的小提琴 – fanta

+0

你沒有做我說的,你沒有創建PostController,你仍然在使用PostsController ,如果你檢查'each'循環,我指定itemController =「post」 – fanta