Ember 1.13 upgradation

72 views Asked by At

I'm upgrading the code from Ember 1.0.4 to Ember 1.13. When I'm execute the below code using ember 1.13 I'm getting the error

title: Ember.computed('content.title', 'managedObject.isHome', 'managedObject.finalManagedObject', {
                set: function(name, value) {
                    this.set('content.title', value);
                },
                if (this.get('content.title') !== undefined) {
                    return title;
                }
                if (this.get('managedObject') == Core.rootNode) {
                    return "Home";
                }
                get: function() {
                  return this.get('managedObject.label') || this.get('managedObject.finalManagedObject.displayName');
                }
            }),

I'm getting the below error while execute the code.

Uncaught SyntaxError: Unexpected token this
3

There are 3 answers

0
Scott Don On BEST ANSWER

I got the answer by using the below code:

title: Ember.computed('content.title', 'managedObject', 'managedObject.label', 'managedObject.finalManagedObject.displayName', {
                set: function(titleKey, newTitle) {
                    this.set('content.title', newTitle);
                    if (newTitle !== undefined) {
                        return newTitle;
                    } else if (this.get('managedObject') === Core.rootNode) {
                        return 'Home';
                    }
                    return this.get('managedObject.label') || this.get('managedObject.finalManagedObject.displayName');
                },      
                get: function() {
                    var title = this.get('content.title');
                    if (title !== undefined) {
                        return title;
                    } else if (this.get('managedObject') === Core.rootNode) {
                        return 'Home';
                    }
                    return this.get('managedObject.label') || this.get('managedObject.finalManagedObject.displayName');
                }
            }),

Thanks for your suggestions Rinold Simon and jelhan.

5
rinold simon On

I think the this reference is lost. Try replacing your code with the below one,

title: Ember.computed('content.title', 'managedObject.isHome', 'managedObject.finalManagedObject', {
  set(name, value) {
    this.set('content.title', value);
  },
  get() {
    return this.get('managedObject.label') || this.get('managedObject.finalManagedObject.displayName');
  }      
  if (this.get('content.title') !== undefined) {
    return title;
  }
  if (this.get('managedObject') == RSuite.rootNode) {
    return "Home";
  }
}),
1
jelhan On

You are using an object to define a computed property. That object must have a get and may have a set function. Both are present. But you have six additional that aren't valid syntax in object definition. You are trying to construct an object like this:

{
  set: function(name, value) {
    this.set('content.title', value);
  },
  if (this.get('content.title') !== undefined) {
     return title;
  }
  if (this.get('managedObject') == RSuite.rootNode) {
    return "Home";
  }
  get: function() {
    return this.get('managedObject.label') || this.get('managedObject.finalManagedObject.displayName');
  }
}

The valid part of that object is:

{
  set: function(name, value) {
    this.set('content.title', value);
  },
  get: function() {
    return this.get('managedObject.label') || this.get('managedObject.finalManagedObject.displayName');
  }
}

Depending on your babel configuration you might be able to simplify it to:

{
  set(name, value) {
    this.set('content.title', value);
  },
  get() {
    return this.get('managedObject.label') || this.get('managedObject.finalManagedObject.displayName');
  }
}

I hope it's clear now. This has nothing to do with upgrade from Ember 1.0.4 to Ember 1.13 in particular. Please have in mind that Ember 1.13 is very old. 2.0 was released more than three years ago. So I would strongly recommend to continue migration until you reach at least to 2.18.