Backbone.Js Empty Array Attribute

Cannot clear Backbone model array attribute

The problem is that you're using

defaults: {
data: []
}

You're giving every instance of loginData access to the same instance of the data array. You can't use objects as defaults in a model's defaults:, or that same object is referenced by every instance of the model.

From the Backbone docs:

Remember that in JavaScript, objects are passed by reference, so if you include an object as a default value, it will be shared among all instances. Instead, define defaults as a function.

Either define defaults as a function, or manually initialize it in initialize:

defaults: {
data: null
},

initialize: function () {
this.set('data', []);
}

As for why your clearSensitiveData isn't working, remember, all instances of your model share the same data after they're instantiated. You're not clearing that shared array, you're just overwriting it in your first instance with a new []. The other model in view2 still points to the original data array, which has not been modified in any way by clearSensitiveData.

Your clearSensitiveData function is fine, you just need to make sure each instance of your model has its own data array.

Backbone outputing empty array for collection.models?

Are you trying to console.log the collection right after executing the fecth() method or waiting for the callback to be executed by using

apples.on('reset', function(){ console.log(this.models);} ,this); 

?

Backbone model default values : null ? empty string ? empty array?

I usually assign null values. I'm unlikely to use null as an actual value to pass around so if something in my application remains null I can tell that something went wrong somewhere.

By the way be careful when assigning empty arrays or objects via the defaults attribute. When you do this the array/object is referenced in each instance, instead of copied to each instance, of your model so they will all modify the same data.

Backbone.js: this.collection.models returns empty array

i quote your code :

success: this.render()

i'm pretty sure you should either write

success:this.render

or

success:function(){
this.render()
}

because by the time you define success callback (and execute it according to your code ), the collection hasn't been fetched yet. There may be other errors though , but this one is pretty obvious

Full Object, but empty array. $.each not hitting

You have an array that you are treating like an object. You should make regionedAps an object, then .each will iterate over it properly.

var regionedAps = {};

I don't think you need $.makeArray()



Related Topics



Leave a reply



Submit