0

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
asked Nov 13, 2010 at 8:58
5
  • 1
    You have not indicated what you expected this code to do nor what it's actually doing. Please clarify. Commented Nov 13, 2010 at 9:05
  • 1
    ObjectManager is missing from the question Commented Nov 13, 2010 at 9:13
  • 1
    Your 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. Commented 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. Commented 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. Commented Nov 13, 2010 at 9:56

3 Answers 3

1

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
answered Nov 13, 2010 at 20:20
Sign up to request clarification or add additional context in comments.

1 Comment

The issue IS that they are not the same object. The logging is to show that they are not the same object.
0

Hmm, your Fieldset constructor is returning an object. Perhaps try calling it as Fieldset({...}) instead of new Fieldset({...})?

answered Nov 13, 2010 at 10:05

7 Comments

Sorry, that was a mistake during editing. Still returns false. (updated)
Also, logging each one individually shows that they contain the same data, but they are not the same object.
Still false. But just out of curiosity, how would that make a difference?
The semantics of new fn() are very different from fn(). Your definition of the function indicates you should call it without new.
I just tested something else which may be useful information.... calling log(ObjectManager.GetFieldset(fieldset.id) == fieldset); just before the return fieldset; of the constructor, evaluates to true....
|
0

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 :-)

answered Nov 13, 2010 at 10:45

2 Comments

Misunderstood the jQuery extend function a little, so I updated my answer. Sorry if it sounds terribly confusing - in short, if your Form class begins with var form = ($.extend(true, { lotsOfStuff }, options); try swapping { lotsOfStuff } and options.
Hmm, I've no idea then. I still think you need to post your Form class as well, though. We have no idea what your Form constructor is doing with the fieldsets handed to it (I still bet it does some copying ;) )

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.