This is my first Ember.js application. I'm building a multiple choice question (eventually a quiz). Whenever the submit button is clicked it should highlight the choice as green for correct or red for incorrect. I get the error "Uncaught TypeError: undefined is not a function" on option.set("highlight", "green") || option.set("highlight", "red) in my controllers/index.js file. When I console.log(option) I can see there is an object with the property highlight. What am I doing wrong?
routes/index.js
var IndexRoute = Ember.Route.extend({
model: function() {
return Ember.A([
{
question: "What's 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");
}
});
});
}
}
});
The JavaScript strict mode exception "can't assign to property" occurs when attempting to create a property on primitive value such as a symbol, a string, a number or a boolean. Primitive values cannot hold any property.
To change a value of the existing property of an object, specify the object name followed by a square bracket, the name of the property you wish to change, an equals sign, and the new value you want to assign.
The JavaScript warning "reference to undefined property" occurs when a script attempted to access an object property which doesn't exist.
To add multiple key/value pairs to an object in the same statement, use the Object. assign() method. The method copies the key/value pairs of one or more objects into a target object and returns the modified target object.
The object option is not an Ember Object so it doesn't have the get
/set
methods.
As Krutius said, you can use Ember.get()
/Ember.set()
to set properties to a plain old JavaScript object or an Ember Object. Example:
Ember.set(myObject, 'property', value);
var val = Ember.get(myObject, 'property');
set
: http://emberjs.com/api/#method_set
get
: http://emberjs.com/api/#method_get
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With