Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way to delete object from enclosing ArrayController

Tags:

ember.js

I have an ArrayController which manages a group of objects. I define an itemController on this, so that each individual object is managed by another ObjectController:

App.IndexController = Ember.ArrayController.extend({
  itemController: "sampleModel"
});

Inside this ObjectController, I have a delete action that is meant to delete objects from the enclosing array, using the "parent" ArrayController:

App.SampleModelController = Ember.ObjectController.extend({
  delete: function() {
    this.get("target.content").removeObject(this.get("content"));
  }
});

I don't want to have a global object store. I'm currently using the target property in order to achieve this kind of behaviour. Is this the idiomatic ember approach?

The complete example can be found here.

like image 697
jcazevedo Avatar asked May 03 '26 14:05

jcazevedo


1 Answers

You can move the delete function to the ArrayController. This way makes sense because the array controller is managing the collection. The way you have it works, but it doesn't seem right because your object is managing the array that it belongs to, which just seems like an awkward way of writing it.

App.IndexController = Ember.ArrayController.extend({
  itemController: "sampleModel",

  delete: function(object) {
    this.get('content').removeObject(object)
  }
});

App.SampleModelController = Ember.ObjectController.extend({});

jsfiddle

like image 115
spullen Avatar answered May 07 '26 09:05

spullen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!