2

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.

4
  • Do you have a collection for the model? It handles this for you. Commented Jan 12, 2014 at 13:02
  • No, I'm trying to fetch and store them in localStorage straight away one by one in above structure. Commented Jan 12, 2014 at 13:09
  • Then why are you using Backbone? Why don't you send a simple Ajax request to the server? Commented Jan 12, 2014 at 13:12
  • well, you're right but I will give it a try using backbone. Commented Jan 12, 2014 at 13:26

1 Answer 1

1

No, I'm trying to fetch and store them in localStorage straight away one by one in above structure

I don't see a specific reason for using Backbone in your case, if the point of using Backbone is just getting JSON data from the server why not using jQuery $.getJSON() function?

$.getJSON('/api/test/employees', function(items) {
   $.each(items, function(_, item) {
       // doSomethingWith(item);
   });
});

If you want to fetch the data using Backbone, you should create a collection:

var EmployeesCollection = Backbone.Collection.extend({
   url: "/api/test/employees",
   model: emp
});

var Employees = new EmployeesCollection();
Employees.fetch({
    success: function(collection) {
      collection.each(function(model) {
          // doSomethingWith(model);
      });
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

it works just like backbone would work. thanks @blacksheep :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.