Here’s a simplified example of an object I have. What I would like to do is when I’m calling the update method, for it to update based on all objects. So, I’ll need to keep track of all objects somehow (can I do this within this Game object? or do I need to wrap this?)
var Game = function() {
this.data = [];
}
Game.prototype = {
add: function(game) {
this.data.push(game);
},
update: function(game) {
for(i = 0; i < this.data.length; i++) {
if(this.data[i].id == game.id) {
this.data[i].name = game.name;
}
}
},
get: function() {
console.log(this.data);
}
}
var g1 = new Game;
g1.add({id: '5', name: 'firstname'});
g1.add({id: '6', name: 'secondname'});
var g2 = new Game;
g2.add({id: '5', name: 'firstname'});
g2.add({id: '6', name: 'secondname'});
g1.update({id: '5', name: 'firstname-updated'})
g1.get();
g2.get();
Basically, if you run this, you’ll see that g1.get() writes the data with the updated id 5, where as g2, will still have it’s original id 5 with name ‘firstname’.
Thanks a lot for any help!
EDIT: Just wanted to add, I would still want there to be multiple objects. It’s not always, that each object will have matching ids. But if they do, they should all update.
You could do something like: