1
votes

I've been searching StackOverflow for an answer to my question, but haven't been able to get something useful yet.

Some background on how I set the collection object up. On fetch, I pass the collection a hash, and set the url as http://localhost/index.php?a=hash. The server should return 12 names for that specific hash.

For some reason, the first model of the collection is always a model with attributes set as a string that was used to fetch the collection data, and every model after that is perfectly fine.

fetch : function(o) {
        var that = this,
            successCallback = o.success,
            errorCallback   = o.error;
        $.ajax({
            type : 'GET',
            url  : this.url + o.hash,
            dataType : 'json',
            success : function(data) {
                _.each(data, function(name) {
                    console.log(name);
                    that.add(new Model({
                        _id : name
                    }));
                });
                successCallback();
            },
            error : errorCallback
        });
    }

List of names that are logged are the 12 that I'm expecting from the server [aaa,bbb,ccc,ddd...]. When I go to render a view using the collection, this is what I have:

Object { cid="c1", attributes={...}, collection={...}, more...
Object { cid="c2", attributes={...}, _changing=false, more...}
Object { cid="c3", attributes={...}, _changing=false, more...}
Object { cid="c4", attributes={...}, _changing=false, more...}
Object { cid="c5", attributes={...}, _changing=false, more...}
Object { cid="c6", attributes={...}, _changing=false, more...}
Object { cid="c7", attributes={...}, _changing=false, more...}
Object { cid="c8", attributes={...}, _changing=false, more...}
Object { cid="c9", attributes={...}, _changing=false, more...}
Object { cid="c10", attributes={...}, _changing=false, more...}
Object { cid="c11", attributes={...}, _changing=false, more...}
Object { cid="c12", attributes={...}, _changing=false, more...}
Object { cid="c13", attributes={...}, _changing=false, more...}

The models that are correct look something like this: http://i.stack.imgur.com/2Fprv.png

The first model of the collection is: http://i.stack.imgur.com/5eGdB.png

It looks like this first model of my collection has an attributes property with the initial hash used to fetch the actual data. I have no idea how it's setting this, or where. Here's how I reference the models of the collection:

render : function() {
        _.each(this.collection.models, function(model) {
            console.log(model);
            var ele = this._build(model.id),
                a   = ele.find('a');

            this.$el.append(ele);
            a.click(App.pageController.trigger('showSetData', model.id));
        }.bind(this));
    }

I could go in and just remove the first model of the collection, but I would like to know why this is happening and if there's a better way of solving this than a hack. Let me know if there's any other information you need! Thanks.

Collection Code

Initialization

var col = new ClientSetList(hash);

ClientSetList Backbone.Collection code

var ClientSetList = Backbone.Collection.extend({
    initialize : function(hash) {
        this.url = App.config.host +
                   App.config.base +
                   App.config.HASH + hash;
    },

    fetch : function(o) {
        var that = this,
            successCallback = o.success,
            errorCallback   = o.error;
        $.ajax({
            type : 'GET',
            url  : this.url,
            dataType : 'json',
            success : function(data) {
                _.each(data, function(setName) {
                    console.log(setName);
                    that.add(new Model({
                        set_name : setName
                    }));
                });
                successCallback();
            },
            error : errorCallback
        });
    }
});
1
Random guess : you're calling your collection constructor with your hash as argument, something like new Yourcollection(hash) - nikoshr

1 Answers

1
votes

The first argument when you init a Collection is the model list, the second is the options hash, unlike views and models which use the first argument as options. What you are describing is consistent with what happens when you omit an empty array when you init the collection.

Can you post the code where you init your collection?

What I think you have:

var opt = {
    //... options
}
var c = new Backbone.Collection(opt);

What you should have:

var opt = {
    //... options
}
var c = new Backbone.Collection([], opt);