0
votes

its me again with another interesting Backbone.js question.

When I call Model#fetch() or Collection#fetch() then Backbone will call the server correct, but it will not store the result, but save works as intended.

In every tutorial I found they explain it that the model will be stored after fetch().

example

response contains 'success' and collection contains the XHR object with the correct json

tagCollection = new CategoryCollection();

tagCollection.fetch({
    complete: function(collection, response) {
        console.log(tagCollection.models);
    },
});

code

/models/category.js

define(['underscore', 'backbone'], function(_, Backbone){

    var CategoryModel = Backbone.Model.extend({
        urlRoot: '/api/v1/category',
    });

    return CategoryModel
});

/collections/category.js

define(['underscore', 'backbone', 'categoryModel'], function(_, Backbone, CategoryModel){

    var CategoryCollection = Backbone.Collection.extend({
        model: CategoryModel,
        url: '/api/v1/category'
    });

    return CategoryCollection
});

/api/v1/category(.json)

[
    {
        id: "1",
        name: "Allgemeines",
        deleted_at: null,
        created_at: "2014-02-23 17:22:22",
        updated_at: "2014-02-23 17:22:22"
    }
]
1

1 Answers

0
votes

Fetch accepts a 'success' callback rather than a 'complete' one. Assuming the data is coming backbone successfully from the server it's probably just not logging out because the callback isn't firing. Try this:

tagCollection.fetch({
    success: function(collection, response) {
        console.log(tagCollection.toJSON());
    },
});