A
allen.leis
Hey all - I was making a few objects using the prototype.js library and
ran into a problem. Specifically I'm trying to create a Squadron class
which contains an array of Platform class. All Squadron classes are
themselves kept in an array in the Squadrons class. However, after I
assign platforms to a specific squadron, all squadrons contain these
platforms! I'm assuming I'm not properly using the prototype or "this"
javascript features. Any help would be appreciated.
- Allen
<script src="scriptaculous/prototype.js"
type="text/javascript"></script>
<script>
var Squadron = Class.create();
Squadron.prototype = {
title : "",
id : "",
platforms : new Array(),
initialize : function(squadron_id, squadron_title) {
this.title = squadron_title;
this.id = squadron_id;
},
get : function (platform_id){
ret = {};
this.platforms.each (function(s) {
ret = (p.id == platform_id) ? p : ret;
});
return ret;
},
add : function(platform_id, platform_title) {
var temp = new Platform(platform_id,platform_title);
this.platforms[this.platforms.length] = temp;
},
dump : function() {
alert("Squadron Title : "+ this.title + "\nId : " + this.id);
this.platforms.each(function(p) {
p.dump();
});
}
};
var Platform = Class.create();
Platform.prototype = {
title : "",
id : "",
initialize : function(platform_id, platform_title) {
this.title = platform_title;
this.id = platform_id;
},
dump : function() {
alert("Platform Title : "+ this.title + "\nId : " + this.id);
}
};
var Squadrons = Class.create();
Squadrons.prototype = {
collection : new Array(),
initialize : function() {
},
add : function(squadron_id, squadron_title) {
var temp = new Squadron(squadron_id, squadron_title);
this.collection[this.collection.length] = temp;
},
get : function (squadron_id){
ret = {};
this.collection.each (function(s) {
ret = (s.id == squadron_id) ? s : ret;
});
return ret;
},
dump : function() {
this.collection.each(function(s) {
s.dump();
});
}
};
var mySquadrons = new Squadrons();
mySquadrons.add(1,"HX-21");
mySquadrons.get(1).add(1,'CH-53e');
mySquadrons.get(1).add(2,'TH-57c');
mySquadrons.add(2,"VX-20");
mySquadrons.dump();
</script>
ran into a problem. Specifically I'm trying to create a Squadron class
which contains an array of Platform class. All Squadron classes are
themselves kept in an array in the Squadrons class. However, after I
assign platforms to a specific squadron, all squadrons contain these
platforms! I'm assuming I'm not properly using the prototype or "this"
javascript features. Any help would be appreciated.
- Allen
<script src="scriptaculous/prototype.js"
type="text/javascript"></script>
<script>
var Squadron = Class.create();
Squadron.prototype = {
title : "",
id : "",
platforms : new Array(),
initialize : function(squadron_id, squadron_title) {
this.title = squadron_title;
this.id = squadron_id;
},
get : function (platform_id){
ret = {};
this.platforms.each (function(s) {
ret = (p.id == platform_id) ? p : ret;
});
return ret;
},
add : function(platform_id, platform_title) {
var temp = new Platform(platform_id,platform_title);
this.platforms[this.platforms.length] = temp;
},
dump : function() {
alert("Squadron Title : "+ this.title + "\nId : " + this.id);
this.platforms.each(function(p) {
p.dump();
});
}
};
var Platform = Class.create();
Platform.prototype = {
title : "",
id : "",
initialize : function(platform_id, platform_title) {
this.title = platform_title;
this.id = platform_id;
},
dump : function() {
alert("Platform Title : "+ this.title + "\nId : " + this.id);
}
};
var Squadrons = Class.create();
Squadrons.prototype = {
collection : new Array(),
initialize : function() {
},
add : function(squadron_id, squadron_title) {
var temp = new Squadron(squadron_id, squadron_title);
this.collection[this.collection.length] = temp;
},
get : function (squadron_id){
ret = {};
this.collection.each (function(s) {
ret = (s.id == squadron_id) ? s : ret;
});
return ret;
},
dump : function() {
this.collection.each(function(s) {
s.dump();
});
}
};
var mySquadrons = new Squadrons();
mySquadrons.add(1,"HX-21");
mySquadrons.get(1).add(1,'CH-53e');
mySquadrons.get(1).add(2,'TH-57c');
mySquadrons.add(2,"VX-20");
mySquadrons.dump();
</script>