I'm trying to fetch data via Backbone JS and get each record from received JSON data. I tried many ways but always ended up with Object object, [] or some random charactors from JSON data.
var emp = Backbone.Model.extend({
urlRoot:"/api/test/employees",
initialize:function () {
}
});
var employees = new emp();
employees.fetch({
success: function (allEmployees) {
$(jQuery.parseJSON(JSON.stringify(allEmployees))).each(function() {
var ID = this.id;
var NAME = this.name;
alert(ID+ NAME);
});
}
});
API output looks like this
[
{
"id": "10",
"firstName": "Kathleen",
"lastName": "Byrne",
"title": "Sales Representative",
"reportCount": "0"
},
{
"id": "9",
"firstName": "Gary",
"lastName": "Donovan",
"title": "Marketing",
"reportCount": "0"
},
{
"id": "7",
"firstName": "Paula",
"lastName": "Gates",
"title": "Software Architect",
"reportCount": "0"
},
{
"id": "11",
"firstName": "Amy",
"lastName": "Jones",
"title": "Sales Representative",
"reportCount": "0"
},
{
"id": "6",
"firstName": "Paul",
"lastName": "Jones",
"title": "QA Manager",
"reportCount": "0"
},
{
"id": "1",
"firstName": "James",
"lastName": "King",
"title": "President and CEO",
"reportCount": "4"
},
{
"id": "3",
"firstName": "Eugene",
"lastName": "Lee",
"title": "CFO",
"reportCount": "0"
},
{
"id": "5",
"firstName": "Ray",
"lastName": "Moore",
"title": "VP of Sales",
"reportCount": "2"
},
{
"id": "2",
"firstName": "Julie",
"lastName": "Taylor",
"title": "VP of Marketing",
"reportCount": "2"
},
{
"id": "12",
"firstName": "Steven",
"lastName": "Wells",
"title": "Software Architect",
"reportCount": "0"
},
{
"id": "4",
"firstName": "John",
"lastName": "Williams",
"title": "VP of Engineering",
"reportCount": "3"
},
{
"id": "8",
"firstName": "Lisa",
"lastName": "Wong",
"title": "Marketing Manager",
"reportCount": "0"
}
]
Where am I doing wrong? How can I get record by record from received JSON data?
exactly like this
{
"id": "10",
"firstName": "Kathleen",
"lastName": "Byrne",
"title": "Sales Representative",
"reportCount": "0"
}
and the next record and so on until the last one, one by one.