2013-10-16 100 views
1

在ember.js文件我已經找到了下一個:Ember.js和選擇多個模型刪除

控制器,讓你與顯示邏輯裝點您的模型。在一般情況下,您的模型將被保存到服務器的屬性,而控制器將有你的應用程序並不需要保存到服務器的性能。

我嘗試添加「選擇」功能,我的應用程序。

這裏是的jsfiddle:http://jsfiddle.net/JWf7X/

似乎濾波器特性由模型濾波,而不是由控制器(因爲執行console.log爲空)。

this.filterProperty('isSelected', true); //managing models 

如何正確寫入removeSelected動作?

是正確的方式在控制器存儲「isSelected」?我想加入isSelected的模型是不正確的做法,因爲這個屬性不會從服務器通過REST API加載並不會被保存到它。

的application.js:

window.App = Ember.Application.create(); 
App.ApplicationAdapter = DS.FixtureAdapter.extend(); 



App.Test = DS.Model.extend({ 
    title: DS.attr('string'), 
}); 


App.Test.FIXTURES = [ 
{ 
    id: 1, 
    title: 'Learn Ember.js', 
}, 
{ 
    id: 2, 
    title: '...', 
}, 
{ 
    id: 3, 
    title: 'Profit!', 
} 
]; 

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
    return this.get('store').find('test'); 
    } 
}); 

App.IndexController = Ember.ArrayController.extend({ 
    actions: { 
     removeSelected: function() { 
     var selected = this.filterProperty('isSelected', true); 
     console.log(selected); 
     } 
    }, 

}); 

App.TestController = Ember.ObjectController.extend({ 
    isSelected: false, 
}); 

的index.html:

<script type="text/x-handlebars" data-template-name="index"> 
    <button {{action "removeSelected"}}>remove selected</button> 
    <ul> 
     {{#each itemController="test"}} 
     <li> 
      {{input type="checkbox" checked=isSelected}} 
      <label>{{title}}</label> 
     </li> 
     {{/each}} 
    </ul> 
</script> 

回答

1

尋找源,使用itemController,在每個視圖幫手。將創建一個新的陣列控制器,而不是使用IndexController。所以isSelected不會出現的IndexController內。

如果設置了itemControllerApp.IndexController你會得到這個工作:

indexController的:

App.IndexController = Ember.ArrayController.extend({ 
    itemController: "test", 
    actions: { 
     removeSelected: function() { 
     var selected = this.filterProperty('isSelected', true); 
     console.log(selected); 
     } 
    } 
}); 

的index.html:

<script type="text/x-handlebars" data-template-name="index"> 
    <button {{action "removeSelected"}}>remove selected</button> 
    <ul> 
     {{#each}} 
     <li> 
      {{input type="checkbox" checked=isSelected}} 
      <label>{{title}}</label> 
     </li> 
     {{/each}} 
    </ul> 
</script> 

這是一個更新的小提琴與此工作http://jsfiddle.net/marciojunior/BKUyk/

+0

日Thnx的幫助。我錯誤地認爲在每個助手中設置itemController會將其設置爲IndexController。 –

+0

不客氣。只是在我的答案更新,'每個itemController =「測試」'是不需要'每個'。小提琴是正確的。 –