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
});
}
});
new Yourcollection(hash)- nikoshr