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
Has Anyone Else Run into Problems Styling Twitter Typeahead's Searchbar
Playing Sound Notifications Using JavaScript
How to Bring a Circle to the Front with D3
Dynamic Template Urls in Angular 2
How to Check If a Custom Protocol Supported
How to Implement Ruby's Extend Module in JavaScript
iOS Uiwebview JavaScript - Insert Data -Receive Callbacks
Angular: Can't Find Promise, Map, Set and Iterator
Detect If Hovering Over Element with Jquery
Set a Request Header in JavaScript
What Is the Lifecycle of an Angularjs Controller
Jquery or JavaScript to Find Memory Usage of Page
Decryption of Aes Created with Sjcl.Js in Ruby
How to Reference Other Properties During Object Declaration in JavaScript