Ember CLI 101 problem

Hi!

I am working through the ember-cli 101 book and I can’t seem to create a new friend.

This is the controller

import Ember from 'ember';

export default Ember.Controller.extend({
  isValid: Ember.computed(
   'model.email',
   'model.firstName',
   'model.lastName',
   'model.twitter',
   function() {
     return
      !Ember.isEmpty(this.get('model.email')) &&
      !Ember.isEmpty(this.get('model.firstName')) &&
      !Ember.isEmpty(this.get('model.lastName')) &&
      !Ember.isEmpty(this.get('model.twitter'));
   }
 ),

  actions: {
     save: function () {
       console.log(this.get('isValid'));
       if (this.get('isValid')) {
         var _this = this;
         this.get('model').save().then(function(friend) {
           _this.transitionToRoute('friends.show', friend);
         });
       } else {
         this.set('errorMessage', 'You have to fill in all fields');
       }
         return false;
     },
     cancel: function() {
               this.transitionToRoute('friends');
               return false;
     }
   }
});

It’s working but I always get the error message ‘You have to fill in all fields’ so I put in the console.log('this.get('isValid')); and it is coming back as undefined therefore dropping into the else branch, however I don’t know why it is not returning true?

Any Ideas?

Thanks

Javascript Automatic Semicolon Insertion (ASI) converts

return
  !Ember.isEmpty(...) &&
  !Ember.isEmpty(...);

to

return;   // <- added semicolon, returns undefined
  !Ember.isEmpty(...) &&
  !Ember.isEmpty(...);

So just change your code to

return !Ember.isEmpty(...) &&
  !Ember.isEmpty(...);

and it will work fine

1 Like

@dpreston thanks a lot it’s been driving me mad!!