EmberJS: programmatically adding a child view to a collection view without changing underlying content

252 views Asked by At

My EmberJS app is confusing me a lot at the moment. I have a collection view, that in turn defines an itemViewClass of a custom view I have defined in my code. Something like:

App.CarouselView = Ember.CollectionView.extend({
    itemViewClass: App.SlideView.extend(),
});

And this CarouselView is rendered inside a template that has a dynamic segment backing it (I hope this makes sense?) . The controller for these dynamic segment is an array controller because the model for these dynamic segments is a collection :) (more confusion, please let me know)

App.SlidesController = Ember.ArrayController.extend();

By now all of you have figured that I am basically rendering a bunch of slides inside of a carousel. And these are dynamically backed in the collectionView by setting the property

contentBinding:'controller' // property set in CarouselView, controller corresponds to SlidesController

The confusion begins now. I want to add a slide to the existing set of slides. So I provide a button with an action : 'add' target='view'

In the SlidesView,

actions:{
   add: function(){
      var carouselView = this.get('childViews')[0]; 
      // SlidesView has carouselView and another view as it's child, hence this.get('childViews')[0] is carouselView
      var newCard = carouselView.createChildView(App.SlideView.extend());
      carouselView.get('childViews').pushObject(newCard);
   }

}

The above piece of code sucks and hurts me bad. I want to basically add a new SlideView to my CarouselView collection programmatically upon a button trigger. But apparently Ember recommends that childViews should not be manipulated directly and I ought to instead change the underlying content. It states in my console.log that manipulating childViews is deprecated etc.

So essentially I need to add something to my content to my SlidesController content ? However, I don't want to add something to the content, this is just a soft add, that is providing the user with a slide so that he may choose to edit or add something if he wants to. He can always discard it. A hard add that will require me to persist the new slide to the DB will come once the user decides to save this information.

0

There are 0 answers