samedi 30 avril 2022

EmberJS model from route returns undefined in controller

Can some one help me on the below doubt?

I am using EmberJS 3.4 version and I have a route which looks like

export default Route.extend({
model(){
    const items = [{price: 10}, {price: 15}]
    return items
 },
});

and a controller which returns undefined for model

export default Controller.extend({

    init(){
        console.log(this.model); //returns undefined
        console.log(this); //has the model object as a property
    },
  })

see this image which contains output

For some reason, this.model returns undefined but when I log "this", it has the model object as the property listed.

My question is, when I access model within a computed property why the property isn't undefined ?

export default Controller.extend({

subtotal: computed('this.model', function(){
     return  this.model.reduce((acc, item) => {
         return acc + item.price
     },0) // return 25
    }),
 })



Aucun commentaire:

Enregistrer un commentaire