I’m in the process of truly learning the nuances of working with JavaScript objects and ran into a snag.
I have a set of “namespaced” objects to segment the DOM and Model to act on. Below is code:
function Sandbox2(){
this.page = {
FirstName: document.getElementById("FirstName")
, LastName: document.getElementById("LastName")
, Email: document.getElementById("Email")
};
this.model = {
FirstName: "James"
, LastName: "Eggers"
, Email: "James.R.Eggers@gmail.com"
};
this.behavior = {};
this.bindPageToModel = function(){
for(var property in this.page){
if (property){
property.value = this.model[property];
}
}
};
this.bindModelToPage = function(){
for(var property in this.model){
if (property){
this.model[property].value = this.page[property];
}
}
};
};
Using JsTestDriver, I’m doing a number of tests to play around and try out a few things of the page and model objects. The specific test is below:
"test ModelBinding should be allowed." : function(){
var sandbox2 = new Sandbox2();
var page = sandbox2.page;
page.FirstName = "Test";
page.LastName = "Account";
sandbox2.bindModelToProperty();
assertEquals("Ensure the new values took.", page.FirstName, sandbox2.page.FirstName);
assertEquals("New Page values should be in the model.", "Test", sandbox2.model.FirstName);
}
In the above test, the first assertEquals passes; however, the second test resolves sandbox2.model.FirstName to “James” (the initial value).
Anyone have any recommendations on how I can change the code (original or test) to allow me to map the page object’s values to the model object?
It seems like the issue is here:
The
propertyvariable is actually the key value of the object (FirstName, LastName and Email). You’re setting thevalueattributes on these string objects without any result.I think you meant to do something like: