简体   繁体   中英

Ember: Return a value or set a model property from Ember promise

Update - more information below

If I have a promise, is it possible to return a value from it?

let itemData = [];
model.data.get('products').then(relatedItems => {
  relatedItems.forEach(function(item,index) {
    console.log(item.get('name')); // Product 1, Product 2 etc
    itemData.pushObject(item);
  });
},reject => {
  console.log('error '+reject);
});

If I try and return the itemData array after the promise has resolved I get undefined.

Alternatively (and preferably) I'd like to be able to set a model property when the promise has resolved:

// component code 

itemData:null,
init() {
  let model = this.get('data');
  model.data.get('products').then(relatedItems => {
     relatedItems.forEach(function(item,index) {
        this.set('itemData',item);
     });
  },reject => {
     console.log('error');
  });
}

The reason for all of this is that I need to sort the product items which I can only access via the promise (in this example). Having set the itemData property I was intending to do something like:

sortedItems:computed.sort('itemData','sortProperties'),
sortProperties:['name:desc']

More information:

In my product route, product.items.item I have a pagination component

 {{pagination-item-trad data=model}}

The model hook in the route product.items.item is

  model(params) {
    let itemModel = this.store.findRecord('product',params.id);
    let mainModel = this.modelFor('product.items');

    return Ember.RSVP.hash({
       data:itemModel,
       mainData:mainModel
    });
} 

The mainModel will include the category model for that particular product item.

Since the product-category model has a many-to-many relationship with products, I need to access the product data in my component using a promise, which was not a problem until I needed to sort the product data. What I am trying to do is obtain the product information from the promise (itemData below) and then use that in the computed property. So the question is how I can extract the data from the promise for use elsewhere in the code? Is there a better way to achieve this? I hope this is clearer!

 sortedItems:computed.sort('itemData','sortProperties'),
 sortProperties:['name:desc']

The component in more detail:

 import Ember from 'ember';
 const {computed} = Ember;

 export default Ember.Component.extend({
   itemData:null,  // i would like to set this within the promise
   sortedItems:computed.sort('itemData','sortProperties'),
   sortProperties:['name:desc'],
   init() {
    let allData = this.get('data');
    let mainModel = allData.mainData;
    var self = this; 
    let itemData = [];
    mainModel.data.get('products').then(relatedItems => {
        relatedItems.forEach(function(item,index) {
        console.log(item.get('name')); // prints Product 1 etc etc
        itemData.pushObject(item);
    });
    self.set('itemData',itemData); // I can't do this
    },reject => {
      console.log('error '+reject);
    });
  }
  // rest of code omitted for brevity
});

Your scope is wrong inside your forEach , this no longer points to your component. You can either use another fat arrow or maintain a reference to the component scope using a variable.

Additionally, I doubt you are meaning to iterate and overwrite itemData on each iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM