ember.js - How to override a property so it returns a different value if the property is empty? -


i going use arraycontroller + itemcontroller setup solve this, maybe better off inside model layer.

i want override property of object return value if property empty. think best described in code (jsfiddle: http://jsfiddle.net/ahx_/tqw4c/2/).

app = ember.application.create()  app.teacher = ember.object.extend()  app.pupil = ember.object.extend({   // todo add property ('answer') returns teacher.answer unless this.answer defined   // pseudo-code:   // answer: function(key, value) {   //  if(ember.isempty(this.answer)) {   //   return this.get('teacher.answer')   //  } else {   //   return this.answer   //  }   // }.property('answer') })  app.indexcontroller = ember.controller.extend({   init: function() {     this._super()     teacher = app.teacher.create({answer: 'correct'})     this.set('pupil1', app.pupil.create({ teacher: teacher, answer: 'incorrect' }))     this.set('pupil2', app.pupil.create({ teacher: teacher }))   } }) 

you need add property .property() cannot refer itself.

object:

app.pupil = ember.object.extend({   answertoshow: function(){     return this.get('answer') ? this.get('answer') : this.get('teacher.answer')   }.property('answer') }) 

template:

<script type="text/x-handlebars" data-template-name="index">   pupil1 ('incorrect'): {{pupil1.answertoshow}}   <br>   pupil2 ('correct'): {{pupil2.answertoshow}} </script> 

demo: http://jsfiddle.net/tqw4c/5/


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -