0

I'm trying to figure out what's wrong about sending action from parentView to one of its children view: I've made a PhotoUploadView used to resize and then upload images; it adds canvas drawing the resized image in a div inside its template:

<div class="img-list">
    //here the canvas will be added
</div>

The action "saveImages" in this view, acts like this:

var list = this.$('.img-list');
$.each(list.children(), function(index, value) {
    //here the code to save
}

But this action is not called directly; because I need to save not only the images but also some records (an offer record and many products records, children of the offer; the images are associated to the product record); So I have the action "save" on the parentView that is like this:

//save records
offer.save().then(function(savedOffer) {
    newProducts.forEach(function(product, indexP) {
        product.set('offer', savedOffer);

        product.save().then(function(savedProduct) {

        }
     }
}

//save photos by cycling the PhotoUploadViews that are inside ProductViews that are inside the mainView
this.get('childViews').forEach(function(view, index) {
    if (index >= 4) {  //the childView from the 4th are the ProductViews
        var productId = view.get('product').get('id');
        var folder = 'offer-' + controller.get('model').get('id') + '/product-' + productId + '/';
        view.get('childViews')[0].send('saveImages', folder, productId);  //the first childView of a ProductView is the UploadView
    }
});

Well, this works and save the images correctly when you add images to an existing offer with existing product; but when you are creating a new offer, it fails since the folder will becone "offer-undefined/product-undefined" because of course you must wait the records to be saved in order to get their ID;

So I'm trying now to move the send action into the .then callback of product save:

var childViews = this.get('childViews'); //the childView starting from the 4th are the productsViews
offer.save().then(function(savedOffer) {
    newProducts.forEach(function(product, indexP) {
        product.set('offer', savedOffer);

            product.save().then(function(savedProduct) {

            var currentPview = (childViews[4 + indexP]); //get the productView associated with the current product
            var productId = savedProduct.get('id');
            var folder = 'offer-' + savedOffer.get('id') + '/product-' + productId + '/';
            currentPview.get('_childViews')[2].send('saveImages', folder, productId); //the object at index 2 of _childViews array is the photoUploadView

Here, the folder is built correctly but after sending action, the saveImages action crashes saying that list is undefined; trying to log the value of "this" inside "saveImages" I can see that also its value is undefined; Someone can please explain why calling the action from one point, it works, and calling it inside the .then callback of product save it doesn't?

I also would like to understand why in the first case I can do

 .get('childViews')[0]

to get the PhotoUploadView, but in the second I must do

 .get('_childViews')[2]

since using get('childViews) it doesn't work anymore; What is the difference between childViews and _childViews? And why _childViews has more elements than childView?

Cereal Killer
  • 3,387
  • 10
  • 48
  • 80
  • I can't quite follow all the code but I believe the scope has changed inside of the promise callback so `this` no longer refers to what you want it to. You should do something like `var self = this;` before you start all the callbacks and then reference `self` inside the callbacks. Here's a very similar question: http://stackoverflow.com/questions/23246373/emberjs-this-changed-in-promise-find-callback – Sarus May 03 '14 at 13:35
  • I've make a mistake: this is not undefined; in both cases this is the same thing (the view instance Ember 641 with the same _context (Ember 615)); but if I log this.$(), in the first case is div#ember641.ember-view, in the second case is undefined.... So saving this in a variable, doesn't change anything... Any other idea? – Cereal Killer May 07 '14 at 00:23

0 Answers0