Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I set this object property?

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");
          }
        });
      });
    }
  }
});
like image 800
ltrainpr Avatar asked Jul 30 '14 14:07

ltrainpr


People also ask

Can't assign to property on not an object?

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.

How do you set property to an object?

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.

Why is object property undefined JavaScript?

The JavaScript warning "reference to undefined property" occurs when a script attempted to access an object property which doesn't exist.

How do I assign an object to a key?

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.


1 Answers

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');

Documentation

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

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

like image 159
Gorzas Avatar answered Oct 23 '22 09:10

Gorzas