I have an ObjectManager, which holds a reference to all objects that are created. The problem is that the ObjectManager is not referencing the object that was created, but instead it seems to be creating a new instance of it. Any help would be appreciated. Thanks!
var Fieldset = function (options) {
var fieldset = ($.extend(true, {
id: '',//Let's assume this has been overridden with 'MyFieldset' via the options param.
title: '',
accordion: '',
fields: [],
hidden: false,
Show: function () { $('#' + this.id).show() },
Hide: function () { $('#' + this.id).hide() }
}, options));
if (fieldset.id != null && fieldset.id != '')
ObjectManager.fieldsets[fieldset.id] = fieldset;//Save a reference to this object in the ObjectManager, so I can call ObjectManager.GetFieldset('MyFieldset'). A reference is only saved if an id is provided.
log(ObjectManager.GetFieldset(fieldset.id) == fieldset);//true
return fieldset;
}
Edit:
Sorry, I thought it was clear what I wanted this to do. There is nothing special about ObjectManger. It just has a property and Get method for each of my objects. For simplicity I only included the fieldsets property and Getter. I hope this clears up my intentions.
var ObjectManager =
{
fieldsets: {},
GetFieldset: function (id) { return this.fieldsets[id]; }
};
Edit2:
After some testing, I found something odd... Hopefully someone can explain to me why this is happening.
var myFieldset = new Fieldset({ id: 'MyFieldset' });
log(myFieldset == ObjectManager.GetFieldset('MyFieldset'));//I just found that it is true in this case...
//... However, this is not the normal way I create Fieldsets... This is:
var editForm = new Form({
dataStore: function () { return ClientsDS; },
id: 'ClientEditForm',
fieldSets: [
new Fieldset({
id: 'ClientDetailsFieldSet',
title: 'Details',
fields: [
new Field({ id: 'ClientID', name: 'ID', property: 'ID', fieldType: 'hidden', value: '0' })
]
})
]
});
log(editForm.fieldSets[0] == ObjectManager.GetFieldset('ClientDetailsFieldSet'));//false
-
1You have not indicated what you expected this code to do nor what it's actually doing. Please clarify.cdhowie– cdhowie2010年11月13日 09:05:40 +00:00Commented Nov 13, 2010 at 9:05
-
1ObjectManager is missing from the questionRob Olmos– Rob Olmos2010年11月13日 09:13:12 +00:00Commented Nov 13, 2010 at 9:13
-
1Your edit does not clear up the question very much. It's still unclear what the code actually does and what you expected it to do instead. Don't make us guess.cdhowie– cdhowie2010年11月13日 09:45:42 +00:00Commented Nov 13, 2010 at 9:45
-
1"The problem is that the ObjectManager is not referencing the object that was created, but instead it seems to be creating a new instance of it." I thought that was clear. I was in the middle of writing a response to you, but decided to do a bit of testing first, which has lead to more detail. I'll update the question. Give me a sec.Brett– Brett2010年11月13日 09:54:29 +00:00Commented Nov 13, 2010 at 9:54
-
It's not clear, because you didn't show us any code that (a) actually adds a fieldset to the manager, only a function that does so but not how the function is called, nor (b) the code that tries to retrieve that fieldset from the manager, and what object it is getting back instead of what you expected.cdhowie– cdhowie2010年11月13日 09:56:16 +00:00Commented Nov 13, 2010 at 9:56
3 Answers 3
On EDIT2:
Your objects are not equal, because they are not the same. The equality operator does not say these two objects have the same key/value pairs, they are equal when they are the same object.
For instance,
var a = b = {a: 1, b:2};
//This is b = {a: 1, b: 2}; a = b; In case you were wondering
a === b //true
var a = {a: 1, b: 2},
b = {a: 1, b: 2};
a === b //false
1 Comment
Hmm, your Fieldset constructor is returning an object. Perhaps try calling it as Fieldset({...}) instead of new Fieldset({...})?
7 Comments
new fn() are very different from fn(). Your definition of the function indicates you should call it without new.I am assuming that your Form class looks something like your Fieldset class, i.e. that it $.extends (makes a deep copy) the options you give it with its internal "prototype". The object returned is the extended prototype not the options extended with the prototype object. Try changing the order of your $.extend arguments (put options second and the internal "prototype" third) and see if that changes anything. Alternatively, post your Form class :-)
2 Comments
var form = ($.extend(true, { lotsOfStuff }, options); try swapping { lotsOfStuff } and options.