Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove elements in content array of ArrayController (Ember)

Tags:

ember.js

I have a model like:

TestModel = Em.Object.create({
    id:'',
    name:''
})

and a object ArrayController like:

testArrayController = Em.ArrayController.create({
    content: [],
    init: function() {
        //push some object TestModel
    }
});

I want to remove some objects depend on id property of a object in content array. How to do that?

like image 941
secretlm Avatar asked May 29 '12 09:05

secretlm


2 Answers

I would use a combination of findProperty and removeObject, see http://jsfiddle.net/pangratz666/rXN4E/:

App.testArrayController = Em.ArrayController.create({
    content: [],

    removeItem: function(propName, value){
        var obj = this.findProperty(propName, value);
        this.removeObject(obj);
    }
});

App.testArrayController.removeItem('id', 42);
like image 190
pangratz Avatar answered Nov 05 '22 21:11

pangratz


I had a situation where I had a table with a checkbox next to each row.

I wanted to remove each row that had been selected when a button was clicked.

Each checkbox was bound to an isSelected property on the item controller.

I used the removeObjects and filterProperty functions to remove the items:

this.removeObjects(this.filterProperty('isSelected'));

Here is a jsbin with an example.

These are the important bits:

App.IndexController = Ember.ArrayController.extend({
  itemController: 'IndexItem',
  actions: {
    removeSelected: function() {
      this.removeObjects(this.filterProperty('isSelected'));
    }
  }
});

App.IndexItemController = Ember.ObjectController.extend({
  isSelected: true
});
like image 25
Zane Avatar answered Nov 05 '22 23:11

Zane