Reputation: 1097
I'm trying to use a Backbone.Model as a ES6 class. This is my code:
import Backbone from 'backbone';
import $ from 'jquery';
Backbone.$ = $;
class DataManager extends Backbone.Model {
url() {
return 'api.json';
}
parse(data) {
console.log(data);
return data;
}
constructor() {
this.fetch()
}
}
export default DataManager;
api.json is a simple json file containing: {test:1}
The fetch
call successfully gets the data (the parse method console.log shows it) but i get the following error:
Uncaught TypeError: Cannot read property 'test' of undefined
The code works if i switch to the es5 syntax: Backbone.Model.extend({...});
Can anyone please explain?
Upvotes: 1
Views: 2416
Reputation: 3411
Don't execute behaviour like that fetch from your constructor. It was put in a method for you to use anywhere but in the constructor. First you create an object and then you tell it what do do. Don't make it start when you create it.
Upvotes: 1
Reputation: 1595
You need to call super()
from inside your constructor in order to call the constructor of the parent class.
Upvotes: 2