I am new to working with backbone and I have been attempting to save my model into a JSON file.
Everything seems to be working fine, until I try to read the JSON output by my backbone application. Upon using Jsonlint, I discovered that my JSON is not valid. Here are some specific details:
Here's how my Model (index.htm) looks like:
//--------------
// Model
//--------------
app.Todo = Backbone.Model.extend({
defaults: {
firstName:'',
lastName:'',
address:'',
occupation:'',
email:'',
idNumber:'',
phoneNumber:''
},
This is the function responsible for saving:
createTodoOnEnter: function(e){
app.todoList.create(this.newAttributes());
// clean input box
firstName: this.firstName.val('');
lastName: this.lastName.val('');
address: this.address.val('');
occupation: this.occupation.val('');
email: this.email.val('');
idNumber: this.idNumber.val('');
phoneNumber: this.phoneNumber.val('');
},
newAttributes: function(){
return {
firstName: this.firstName.val().trim(),
lastName: this.lastName.val().trim(),
address: this.address.val().trim(),
occupation: this.occupation.val().trim(),
email: this.email.val().trim(),
idNumber: this.idNumber.val().trim(),
phoneNumber: this.phoneNumber.val().trim()
}
}
The resulting JSON when saved is as follows:
{
firstName: "ramy",
lastName: "dabbabi",
address: "50 rue Fadhel ben achour",
occupation: "Developer",
email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdbfaca0b48daaa0aca4e3aea2a0">[email protected]</a>",
idNumber: 09008585,
phoneNumber: 789456123
}
You may notice that the attributes (firstName, lastName, etc.) are missing double quotes which is causing the JSON to be invalid.
Is there a way to fix this issue? Is there a built-in function in backbone that allows me to define the JSON format?
Can I customize the JSON output created by the this.model.create function?