Why can't I set the property of this object?

This is my first Ember.js application. I am creating a multiple choice question (after all, a quiz). Whenever the submit button is pressed, it should highlight the selection as green for right or red for wrong. I get the error "Uncaught TypeError: undefined is not a function" on option.set ("highlight", "green") || option.set ("highlight", "red") in the file of my controllers /index.js. When I console.log (option), I see that there is an object with a property selected. What am I doing wrong?

routes / index.js

var IndexRoute = Ember.Route.extend({ model: function() { return Ember.A([ { question: "What up?", answer: 'option b', options: [ { text: "option a", active: false, highlight: '' }, { text: "option b", active: false, highlight: '1' } ] }, { question: "How many?", answer: 'two', options: [ "one", "two" ] } ]); } }); 

Controllers / index.js

 var IndexController = Ember.ObjectController.extend({ actions:{ submitAction : function(){ this.get('model').forEach(function (item){ item.options.forEach(function (option) { if (option.text === item.answer) { console.log(option); option.set("highlight", "green"); console.log(option.highlight); } if (option.active && (option.text !== item.answer)) { option.set("highlight", "red"); } }); }); } } }); 
+7
javascript
source share
1 answer

The object option is not an Ember object, so it does not have get / set methods.

As Krutius said, you can use Ember.get() / Ember.set() to set properties for a plain old JavaScript object or an Ember object. Example:

 Ember.set(myObject, 'property', value); var val = Ember.get(myObject, 'property'); 

Documentation

set : http://emberjs.com/api/#method_set

get : http://emberjs.com/api/#method_get

+16
source share

All Articles